Skip to content

Instantly share code, notes, and snippets.

@guolinaileen
Last active December 14, 2015 21:09
Show Gist options
  • Save guolinaileen/5149285 to your computer and use it in GitHub Desktop.
Save guolinaileen/5149285 to your computer and use it in GitHub Desktop.
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