Java Solution very easy to understand and efficient


  • 0
    S

    class Solution {
    public void sortColors(int[] a) {

    int l=0,mid=0,h=a.length-1;
    while(mid<=h)
    
    	
    {
    	
    switch (a[mid]) {
    case 1:
    {
    mid++;
    	break;
    }
    

    case 0:
    {

    int t=a[l];
    a[l]=a[mid];
    a[mid]=t;
    mid++;
    l++;
    	break;
    

    }
    case 2:
    {

    int t=a[mid];
    a[mid]=a[h];
    
    a[h]=t;
    
    h--;
    
    break;
    
    }	
    	
    }	
    	
    	
    }
    }
    }

Log in to reply
 

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