码迷,mamicode.com
首页 > 其他好文 > 详细

荷兰国旗问题 划分成3部分 leecode

时间:2014-07-05 21:34:36      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   问题   io   div   

 1 public class Solution {
 2     public void sortColors(int[] A) {
 3         int len=A.length;
 4         int beg=0;
 5         int end=len-1;
 6         int cur=0;
 7         while(cur<=end){
 8           if(A[cur]==0)
 9           {
10               swap(A,cur,beg);
11               beg++;
12               cur++;
13               
14               
15           }
16           else if(A[cur]==1)
17           {
18               cur++;
19               
20           }
21           else
22           {
23               swap(A,cur,end);
24               end--;
25               
26              
27           }
28             
29         
30         }
31     }
32         public void swap(int[] A,int i,int j)
33         {
34             int temp=A[i];
35             A[i]=A[j];
36             A[j]=temp;
37             
38             
39             
40         }
41        
42         
43         
44     }

 

荷兰国旗问题 划分成3部分 leecode,布布扣,bubuko.com

荷兰国旗问题 划分成3部分 leecode

标签:style   blog   color   问题   io   div   

原文地址:http://www.cnblogs.com/hansongjiang/p/3826264.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!