Easy-to-understand Three-Way-Partition inspired by 3-way-quickSort


  • 0
    Z

    Easy-to-understand Three-Way-Partition inspired by 3-way-quickSort

    public class Solution {
        public void sortColors(int[] nums) {
            int l = 0,r = nums.length-1,i=0;
            while(i<=r){
                if(nums[i]==0){swap(i++,l++,nums);}
                else if(nums[i]==2){swap(i,r--,nums);}
                else i++;
            }
        }
        private void swap(int i, int j, int[] nums){
            if(i==j)return;
            nums[i]^=nums[j];
            nums[j]^=nums[i];
            nums[i]^=nums[j];
        }
    }
    

Log in to reply
 

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