[leetcode] 题型整理之排序

时间:2022-09-23 18:14:23

75. Sort Colors

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.

Follow up:
A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.

Could you come up with an one-pass algorithm using only constant space?

public class Solution {
public void sortColors(int[] nums) {
int length = nums.length;
int bluePos = length - 1;
while (bluePos >= 0 && nums[bluePos] == 2) {
bluePos--;
}
int redPos = 0;
while (redPos <= bluePos && nums[redPos] == 0) {
redPos++;
}
for (int i = redPos; i <= bluePos; i++) {
int x = nums[i];
if (x == 2) {
if (i == bluePos) {
break;
} else {
nums[i] = nums[bluePos];
nums[bluePos] = 2;
while (bluePos >= redPos && nums[bluePos] == 2) {
bluePos--;
}
i--;
}
} else if (x == 0) {
if (i == redPos) {
redPos++;
} else {
nums[i] = nums[redPos];
nums[redPos] = 0;
while (redPos <= bluePos && nums[redPos] == 0) {
redPos++;
}
}
}
}
}
}

[leetcode] 题型整理之排序的更多相关文章

  1. &lbrack;leetcode&rsqb; 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  2. &lbrack;leetcode&rsqb; 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  3. &lbrack;leetcode&rsqb; 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  4. &lbrack;leetcode&rsqb; 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  5. &lbrack;leetcode&rsqb; 题型整理之数字加减乘除乘方开根号组合数计算取余

    需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...

  6. &lbrack;leetcode&rsqb; 题型整理之cycle

    找到环的起点. 一快一慢相遇初,从头再走再相逢.

  7. &lbrack;leetcode&rsqb;题型整理之用bit统计个数

    137. Single Number II Given an array of integers, every element appears three times except for one. ...

  8. &lbrack;leetcode&rsqb; 题型整理之查找

    1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...

  9. &lbrack;leetcode&rsqb; 题型整理之字符串处理

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = &q ...

随机推荐

  1. WEP算法的安全性

    翻译自http://www.isaac.cs.berkeley.edu/isaac/wep-faq.html WEP算法的安全性 这篇文章讲述了我们对有线等效加密协议(WEP, 802.11标准的一部 ...

  2. 用JQ仿造百度书籍预售页面的单屏滚页效果

    今天的项目需要做到一个介绍页面,我主动提出走单屏滚页的风格,毕竟交互性好,逼格也高,具体效果可以参照百度知道书籍预售页面. 其实现效果就大概是这样的: 还是老样子,所有步骤文件可以从我的Github上 ...

  3. MES: ESB

    ESB定义了消息的收发和收发池,对于各种通讯方式定义了收发API,在收到信息后由eventBus来发布消息 ISender: public abstract interface ISender { p ...

  4. cocos2d-x混合BlendFunc的使用

    1.什么是混合模式 “混合”是指两种颜色的叠加方式.在新图片将要渲染画到屏幕上的时候,将用在新图片中的红.绿.蓝和透明度信息,与屏幕上已经存在的图片颜色信息相融合. 说的具体一点,就是把某一像素位置上 ...

  5. opencv 图像仿射变换 计算仿射变换后对应特征点的新坐标 图像旋转、缩放、平移

    常常需要最图像进行仿射变换,仿射变换后,我们可能需要将原来图像中的特征点坐标进行重新计算,获得原来图像中例如眼睛瞳孔坐标的新的位置,用于在新得到图像中继续利用瞳孔位置坐标. 仿射变换在:http:// ...

  6. hdu2208之搜索

    唉,可爱的小朋友 Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 用sql语句按周、按月、按季、按年统

    原文地址:http://hi.baidu.com/%BD%F0%D3%F1kl_y/blog/item/1c368ffba9388476024f5645.html --按mySql语法统计按周,月,季 ...

  8. C&num;版微信公众号支付&vert;微信H5支付&vert;微信扫码支付问题汇总及解决方案总结

    最近负责的一些项目开发,都用到了微信支付(微信公众号支付.微信H5支付.微信扫码支付).在开发的过程中,在调试支付的过程中,或多或少都遇到了一些问题,今天总结下,分享,留存.代码在文章结尾处,有需要的 ...

  9. SIFT算法大综合

     SIFT算法原理+参看资料+问题issue 参考书籍——<图像局部不变性特征与描述>王永明.王贵锦著 SIFT特征点提取——详见博客:https://blog.csdn.net/ling ...

  10. Specified version of key is not available &lpar;44&rpar;

    -- ::, ERROR [HiveServer2-Handler-Pool: Thread-]: transport.TSaslTransport (TSaslTransport.java:open ...