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.

Note:
You are not suppose to use the library's sort function for this problem.

三个指针 lo =cur =0,hi =n-1

a[cur]==0: if(lo==cur) cur++ lo++

     else: swap(lo,cur) lo++

 class Solution {
public void sortColors(int[] nums) {
int begin = 0;
int cur = 0;
int end = nums.length-1;
while(cur<=end){
if(nums[cur]==2){
swap(nums,cur,end);
end--;
}
else if(nums[cur]==1){
cur++;
}
else //(nums[cur]==0)
{
if(nums[cur]!=nums[begin])
swap(nums,cur,begin);
begin++;
cur++;
}
}
}
private void swap(int[] nums,int i ,int j){
int temp;
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

最新文章

  1. TRF7960天线参数试验
  2. Android_Spinner_example
  3. Linux下使用openvpn客户端
  4. awk-模式匹配
  5. tls session resumption
  6. php基础。php与js的不同
  7. 巧用FineReport搭建成本管控监测系统
  8. python3元组
  9. Android/Linux boot time分析优化
  10. URAL 1183 Brackets Sequence
  11. python-day54--前端之js-DOM对象
  12. 【bzoj2693】jzptab 莫比乌斯反演+线性筛
  13. 关于createTextRange和createRange的一些用法【转】
  14. springmvc 路由
  15. 配置ntpd时钟同步服务
  16. hdu 1087 简单dp
  17. Jsp 公用标签库
  18. Spark源码分析 &ndash; Checkpoint
  19. Python中的数据结构 --- 元组(tuple)、字典(tuple)
  20. web.xml中不同版本的servlet头以及版本控制

热门文章

  1. Linux网络实时监控配置
  2. android camera之nv21旋转
  3. 扩张js的String——trim
  4. scheme 中的宏使用
  5. 微软官方SqlHelper类 数据库辅助操作类
  6. 尼康D90多点对焦
  7. ios --转载-从URL中截取所包含的参数,并且以字典的形式返回和参数字典转URL
  8. Toxophily-数论以及二分三分
  9. 浅析PageRank算法(转)
  10. 《从零开始学Swift》学习笔记(Day 59)——代码排版