A traditional solution by java


  • 0
    C

    public class Solution {
    public int [] moveZeroes(int[] nums) {
    int sum=0;
    int sum0=0;
    //base case
    if(nums.length==1){return nums;}
    else
    {
    for(int n = 0;n<nums.length;n++)
    {
    if(nums[n] == 0)
    {
    sum0++;
    }
    }
    for(int i=0;i<nums.length-sum;i++)
    { while(nums[i] == 0){
    if(nums[i] == 0)
    {
    if(i==nums.length-1){return nums;}
    sum++;
    if(sum>nums.length){break;}
    if(sum > sum0){break;}
    int k=i;
    for(int j=k+1;j<nums.length;j++)
    {
    nums[k]=nums[j];
    k++;
    }
    nums[nums.length-sum]=0;
    }
    }
    }
    return nums;
    }
    }
    }


Log in to reply
 

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