阅读背景:

移除0元素,把0元素按相对位置放到数组最后

来源:互联网 
/*
 * Given an array nums, write a function to move all 0's to the end of it while maintaining the 
 * relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
 * */
public class Solution {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[]  aa ={0,1,0,3,12};
		int[] res = Solution.moveZeroes(aa);
		for(int x : res)
			System.out.print(x+"   ");
 	}
	 public static int[] moveZeroes(int[] nums) {
	        int start = 0;//记录0的个数
	        int num = nums.length;
	        int[] res = new int[num];
	        for(int i = 0;i<num;i++){
	        	 if(nums[i]!=0)
	        	 {
	        		 nums[start]=nums[i];
	        	     start++;
	        	 }
	        }
	        for(int i = num-1;i>=start;i--)
	        	nums[i] = 0;
	        res = nums;
	        return res;
	    }
}
/*
 * Given an array nums, write a function to 



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: