Java o(n) solution


  • 0
    N

    public class Solution {
    public void moveZeroes(int[] nums) {
    int k =0, j =0;
    boolean isZero = false;
    for(int i = 0; i < nums.length; i++){
    if(nums[i] == 0 && !isZero){
    isZero = true;
    k = i;
    }
    if(nums[i] != 0 && isZero){
    int temp = nums[i];
    nums[i] = nums[k];
    nums[k] = temp;
    k++;
    }
    }
    }
    }

    • >! list item


Log in to reply
 

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