[leetcode] 题型整理之排序
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] 题型整理之排序的更多相关文章
- [leetcode] 题型整理之排列组合
一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...
- [leetcode] 题型整理之图论
图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...
- [leetcode] 题型整理之二叉树
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- [leetcode] 题型整理之动态规划
动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...
- [leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余
需要注意overflow,特别是Integer.MIN_VALUE这个数字. 需要掌握二分法. 不用除法的除法,分而治之的乘方 2. Add Two Numbers You are given two ...
- [leetcode] 题型整理之cycle
找到环的起点. 一快一慢相遇初,从头再走再相逢.
- [leetcode]题型整理之用bit统计个数
137. Single Number II Given an array of integers, every element appears three times except for one. ...
- [leetcode] 题型整理之查找
1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...
- [leetcode] 题型整理之字符串处理
71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example,path = &q ...
随机推荐
- [noip科普]关于LIS和一类可以用树状数组优化的DP
预备知识 DP(Dynamic Programming):一种以无后效性的状态转移为基础的算法,我们可以将其不严谨地先理解为递推.例如斐波那契数列的递推求法可以不严谨地认为是DP.当然DP的状态也可以 ...
- noi-openjudge[4.7搜索]怀表问题
为啥我觉得这是个DP-.f[i][j][k][l]表示四种零件分别用了i,j,k,l个的方案数.然后发现这样不能保证表一定能接在表链首尾,也不知道状态之间如何转移,那么加一维变成f[i][j][k][ ...
- edit界面初始化加默认值
功能名称:modelCreateAction 切入类型:after 事件名称:com.kingdee.bos.eventbus.event.model.ModelCreateEvent package ...
- BZOJ1017: [JSOI2008]魔兽地图DotR
传送门 设$f[i][j][k]$表示对于第$i$个点,向父节点贡献$j$个已合成的装备,花费了$k$的代价,最多获得的力量值. 单纯的$f[i][j][k]$是很难转移的,主要原因是无法维护和其他儿 ...
- 一道关于阿里简单面试题的反思C/C++
题目是这样的: 正在挑战一个CrackMe的你,把需要填写的前面几位密码都正确猜出了,可是这最后一位密码,好像藏得有点深.CrackMe的作者还挑衅般的在里面藏了个.tar.gz文件,解压缩出来,里面 ...
- 树莓派2安装使用小米WIfi(360 小度 腾讯wifi)
更新2015年11月16日,jessie内核版本号4.1.13(uname -a 可以查看)直接可以驱动MT7601U,无需手动编译. 截止2015-4-6,本文基于树莓派2,raspbian,内核版 ...
- 最简单的STM32入门教程----闪烁LED
本文讲述的是如何从零开始,使用keil建立一个简单的STM32的工程,并闪烁LED灯,给小白看. 第零步,当然首先你得有一个STM32的板子,其IO口上接了一个LED... 第一步,建立一个文件夹0. ...
- [Nhibernate]二级缓存(二)
目录 写在前面 文档与系列文章 更新数据 二级缓存管理 总结 写在前面 本篇文章也算nhibernate入门系列的结尾了,在总结nhibernate系列的过程中,遇到了很多问题,学习的过程也是解决bu ...
- Bash 中为 _ 变量赋空值的三个场景
$_ 有好几个功能,我们最常用的是用它来获取“刚刚执行过的命令的最后一个参数”这个功能,比如下面这样: $ ls ~/Downloads/very/long/dir/ # ls 到某个目录看看有没有 ...
- Android网络请求通信之Volley
一.Volley简介 Volley网络框架是Google公司在2013年发布的一款Android平台上的网络请求通信库.以下是对Volley的简单归纳. Volley的优点: 使网络通信更快.更简单. ...