首页 > 其他 > 详细

Sort Colors

时间:2014-02-12 12:03:31      阅读:354      评论:0      收藏:0      [点我收藏+]

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

bubuko.com,布布扣
 1 public class Solution {
 2     public void sortColors(int[] A) {
 3         int i=0,start=0,end =A.length-1;
 4         while(i<=end){
 5             if(A[i]==0){
 6                 int temp = A[i];
 7                 A[i] = A[start];
 8                 A[start] = temp;
 9                 i++;
10                 start++;
11                 continue;
12             }
13             else if(A[i]==2){
14                 int temp = A[i];
15                 A[i] = A[end];
16                 A[end] = temp;
17                 end--;
18                 continue;
19             }
20             else{
21                 i++;
22             }
23         }
24     }
25 }
View Code

Sort Colors

原文:http://www.cnblogs.com/krunning/p/3545273.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!