C++ easy understand


  • 0
    B
    class Solution {
    public:
    	void moveZeroes(vector<int>& nums) {
    		int n = nums.size();
    		int idx0 = 0, idx1 = 0;
    		while (idx0 < n && idx1 < n) {
    			while (idx1 < n && nums[idx1] == 0) {
    				idx1++;
    			} // find next non-zero number.
    			while (idx0 < n && nums[idx0] != 0) {
    				idx0++;
    			} // find next zero number.
    			if (idx0 < idx1 && idx1 < n) {
    				swap(nums[idx0], nums[idx1]);
    			}
    			idx1++; // look for next non-zero.
    		}
    	}
    };

Log in to reply
 

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