sort colors

 1 class Solution {
 2 public:
 3     void sortColors(int A[], int n) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int left = 0;
 7         int right = n-1;
 8         
 9         int i=0;
10         int j = n-1;
11         
12         while( i <= right )
13         {
14             if( A[i] == 0 ) 
15             { 
16                 A[i] = A[left];
17                 A[left] = 0;
18                 left++;
19                 i++;
20             }
21             else if(A[i] == 1 ) { i++; }
22             else
23             {
24                 A[i] = A[right];
25                 A[right] = 2;
26                 right --;
27             }
28         }
29         return;
30     }
31 
32 };

 

posted on 2013-09-03 19:33  jumping_grass  阅读(139)  评论(0)    收藏  举报

导航