Last active
December 14, 2015 21:09
-
-
Save guolinaileen/5149285 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public void sortColors(int[] A) { | |
// Start typing your Java solution below | |
// DO NOT write main() function | |
int end=A.length; | |
int start=-1; | |
for(int i=0; i<end; i++) | |
{ | |
if(A[i]==0) | |
{ | |
start++; | |
swap(A, i, start); | |
} | |
if(A[i]==2) | |
{ | |
end--; | |
swap(A, i, end); | |
i--; | |
} | |
} | |
} | |
void swap(int [] A, int i, int j) | |
{ | |
int temp=A[i]; | |
A[i]=A[j]; | |
A[j]=temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment