[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 ...
随机推荐
- 菜鸟的Python学习之路(流水账)
揭开Python的面纱 开始是因为别人说Python简单才开始学的,然后那段时间刚考完研,也没什么事,就多少瞅了瞅,然后发现语法的确简单很多,或者说简洁更合适. 当时看的是简明Python教程,没用多 ...
- ES5语法
ES5新语法主要是体现在Object和.Array操作,同时涉及到JSON. Function.Date 和 String类型上. 1.Object ES5最大的特点是对象扩展很多方法. 新建对象:c ...
- EntityFramework 数据库的迁移
第一步:在程序包管理器控制台里: Enable-Migrations -ProjectName EF所在的项目名称 第二步:运行后会在字段生成Migrations文件夹,Migrations-> ...
- 入门: 使用JNI 从C++代码中调用Java的静态方法
开发环境: 操作系统: (uname -a output) Linux ubuntu 3.8.0-19-generic #29-Ubuntu SMP Wed Apr 17 18:16:28 UTC ...
- Linux设置Memcached开机启动
Memcached开机启动方式 方法一: 在 /etc/rc.d/rc.local 文件中追加启动命令 /usr/local/memcached/bin/memcached -u root -d - ...
- 关于DataTable添加新列到指定列的方法
在开发新项目的时候发现了一个问题 dtResult.Columns.Add()方法只能将指定的列添加到DataTable的列的最后的位置,但是不能添加到指定的列上.举例来说,假设dtResult总共有 ...
- iOS开发——高级篇——图片轮播及其无限循环效果
平时APP中的广告位.或者滚动的新闻图片等用到的就是图片轮播这种效果,实现方式主要有两种,一种是ScrollView+ImageView,另一种则是通过CollectionView,今天总结的是Scr ...
- linux常用命令-文件搜索命令-find
find [目录] [选项] 文件名或者正则表达式 -name 根据文件名搜索 -iname 搜索文件名的时候忽略大小写 例:find /etc -name init find /etc -i ...
- CTR预估评价指标介绍
1 离线指标 1.1 LogLoss 1.1.1 KL散度 logloss使用KL散度来计算.设样本的真实分布为P,预测分布为Q,则KL散度定义如下: 这里可以通俗地把KL散度理解为相同事件空间里两个 ...
- 关于oracle中日期使用
spl> select * from emp where dates between to_date('2007-06-12 10:00:00' ...