问题描写叙述:

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.

基本思想:

遍历一遍记录0,1,2的个数,然后利用计数排序写出代码。

代码:

void sortColors(int A[], int n) {  //C++int rNum =0,wNum = 0, bNum =0; for(int i = 0; i < n; i++){ if(A[i] == 0) rNum++; else if(A[i] == 1) wNum++; else bNum++; } int rBegin = 0;  int wBegin = rNum;  int bBegin = rNum+wNum; //ith element i < rNum  for(int i = 0; i < n; i++){ if(i < rNum) A[i] =0; else if(i < n-bNum) A[i] = 1; else A[i] =2; }
}

one-pass algorithm

void sortColors(int[] A) {int i=-1, j=-1, k=-1;for(int p = 0; p < A.length; p++){if(A[p] == 0){A[++k]=2;A[++j]=1;A[++i]=0;}else if (A[p] == 1){A[++k]=2;A[++j]=1;}else if (A[p] == 2){A[++k]=2;}}}

[leetcode]Sort Colors相关推荐

  1. LeetCode Sort Colors

    1.题目 Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  2. LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)

    LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...

  3. 【排序】LeetCode 75. Sort Colors

    LeetCode 75. Sort Colors Solution1: 参考自:<leetcode-cpp 答案> 由于0,1,2非常紧凑,首先想到计数排序(counting sort), ...

  4. Leetcode 075 Sort Colors

    题目连接:Leetcode 075 Sort Colors 解题思路:从左向右遍历,同时维护两个指针r和b,0~r位置上的元素为0,b~n-1位置上的为2.在遍历过程中,碰到0,放到r的位置:碰到2, ...

  5. [LeetCode]75.Sort Colors

    [题目连接] 75.Sort Colors [题目] Given an array with n objects colored red, white or blue, sort them so th ...

  6. Sort Colors - LeetCode

    Sort Colors - LeetCode 题目: Given an array with n objects colored red, white or blue, sort them so th ...

  7. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  8. 82 颜色分类II-彩虹排序(Sort Colors II)

    文章目录 1 题目 2 解决方案 2.1 思路 2.2 图解 2.3 时间复杂度 2.4 空间复杂度 3 源码 1 题目 题目:颜色分类II(Sort Colors II) 描述:给定一个有n个对象( ...

  9. Sort Colors leetcode java

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

最新文章

  1. EntityModelStudio系列教程5--EMLib框架之Eql
  2. MySQL中一个双引号错位引发的血案
  3. CodeForces - 1451E2 Bitwise Queries (Hard Version)(交互+构造+位运算)
  4. Web应用开发中的几个问题
  5. PWA · 前后端协作 · Node | JTalk 掘金线下活动第七期
  6. dataBinding和retrofit的使用
  7. 记一次惨烈的电话面试题
  8. Windows中使用http-server搭建一个本地服务
  9. carrot2聚类的不同聚类算法 选用方法
  10. GreenSock2
  11. Lodop6.226_Clodop3.075.zip
  12. 仿京东商城主页(静态代码)
  13. 30本引进翻译版日本计算机技术书籍推荐
  14. android qq 功能,Android-类qq功能(一)
  15. MCS51 程序存储器(ROM)
  16. BLE Mesh网络协议综述
  17. MIT新任女校长震撼北美高校圈!61岁的她曾是杜克首位女教务长
  18. 警告:Google公开Windows零日漏洞
  19. 【干货】Android实现支付宝当面付
  20. 国内外关于文物安全的法律法规、政策、标准等公开文件收集

热门文章

  1. java学习之路--零碎的知识笔记
  2. 无法启动MySQL数据库
  3. SAP S/4HANA CDS View的访问控制实现:DCL介绍
  4. javascript中while循环、do....while与for循环的用法和区别
  5. 文件的特殊权限:suid sgid sticky
  6. 【ASP.NET开发】ASP.NET对SQLServer的通用数据库访问类
  7. Android -- Intent
  8. windows installer清理实用工具
  9. 《head first java 》读书笔记
  10. ACCP学习旅程之-----基础篇