Java Solution - using streams


  • 0
    W
    public void moveZeroes(int[] nums) {
            
            ArrayList<Integer> listNums = new ArrayList<>();
    		for (int i : nums)
    		{
    			listNums.add(Integer.valueOf(i));
    		}
    				
    		ArrayList<Integer> zeros = (ArrayList<Integer>) listNums.stream().filter(u -> u == 0 ).collect(Collectors.toList());
    		ArrayList<Integer> nozeros = (ArrayList<Integer>) listNums.stream().filter(u -> u!= 0 ).collect(Collectors.toList());
    		nozeros.addAll(zeros);
    		int i=0;
    		for (Object oneNumber : nozeros.toArray())
    		{
    			nums[i++] = (int) oneNumber;
    		}
            
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.