C# Solution Move Zeros


  • 0
    A
    public void MoveZeroes(int[] nums) {
           int lastIndex = nums.Length - 1;
                int zeroCounter = 0;
                for (int i = 0; i < lastIndex; i++)
                {
                    if ((nums[i] == 0))
                    {
                        do
                        {
                            if (zeroCounter + i >= nums.Length)
                                break;
                            for (int j = i; j < lastIndex; j++)
                            {
                                nums[j] = nums[j + 1];
                            }
                            
                            nums[lastIndex - zeroCounter] = 0;
                            zeroCounter++;
                            
                        } while (nums[i] == 0);
                    }
                    if (i == lastIndex - zeroCounter)
                        break;
                }
        }
    

Log in to reply
 

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