C# Solution good example for .net developers


  • 0
    B

    public class Solution
    {

    public void MoveZeroes(int[] nums)
        {
            if (nums.Length == 0 || nums.Length == 1) return;
    
            int countZeroes = 0;
    
            for (int i =0; i< nums.Length; i++)
            {
                //we need to count the numbers of zeroes and continue
                if (nums[i] == 0)
                {
                    countZeroes++;
                    continue;
                }
    
                if (nums[i] != 0 && countZeroes > 0)
                {
                    // you can do a swap
                    nums[i - countZeroes] = nums[i];
                    // the new value must be zero
                    nums[i] = 0;
                }
            }
        }
    

    }


Log in to reply
 

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