C++ code with two pointer


  • 0
    H
    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
             int n = nums.size();
    	     int left = 0, right = 0;
             while (right < n){
    		     if (nums[right] != 0 && nums[left] == 0){
    			 swap(nums[left], nums[right]);
    			 left++;
    		 }
    		 if (nums[left] != 0)
    			 left++;
    		 right++;
    	 }
      }
    };

Log in to reply
 

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