143. Sort Colors II
最后更新
一刷
class Solution {
public void sortColors2(int[] colors, int k) {
// write your code here
if (colors.length <= 1) return;
int start = 0;
int end = colors.length - 1;
int min = 1;
int max = k;
while (start < end) {
int temp = start;
while (temp <= end) {
if (colors[temp] == min) {
swap(start ++, temp ++, colors);
} else if (colors[temp] == max) {
swap(end --, temp, colors);
} else {
temp ++;
}
}
min ++;
max --;
}
}
public void swap(int l, int r, int[] colors) {
int temp = colors[l];
colors[l] = colors[r];
colors[r] = temp;
}
}
143. Sort Colors II的更多相关文章
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
- LeetCode: Sort Colors 解题报告
Sort ColorsGiven an array with n objects colored red, white or blue, sort them so that objects of th ...
- Sort Colors I & II
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- 【LeetCode】Sort Colors
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 52. Sort Colors && Combinations
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 【LeetCode】75. Sort Colors (3 solutions)
Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects of t ...
随机推荐
- 正则表达式中的\n
搜索文件中的字符,希望每次从每行的开始进行匹配. 所以在表达式开头加了\n 结果发现怎么都匹配不了. string regEx = @"\n\d*\s*!\s*TESTNAME” 最后,偶然 ...
- HTTP错误代码详细介绍
HTTP 400 - 请求无效 HTTP 401.1 - 未授权:登录失败 HTTP 401.2 - 未授权:服务器配置问题导致登录失败 HTTP 401.3 - ACL 禁止访问资源 HTTP 40 ...
- POJ 1275 Cashier Employment(差分约束)
http://poj.org/problem?id=1275 题意 : 一家24小时营业的超市,要雇出纳员,需要求出超市每天不同时段需要的出纳员数,午夜只需一小批,下午需要多些,希望雇最少的人,给出每 ...
- js设置radio选中
在页面数据绑定时,经常会遇到给radio设置选中,以下是我写的js方法,经测试可以使用.欢迎拍砖 <html> <head> <script type="tex ...
- eCos中断模型
http://blog.csdn.net/chychc/article/details/8313458 http://www.cnblogs.com/RandyQ/archive/2013/04/14 ...
- 【POJ】1054 The Troublesome Frog
题目是非常经典的搜索+剪枝.题意简言之就是,青蛙需要沿着直线踩着踏点通过田地,并且踏点需要至少为3.问哪条路径青蛙踩坏的作物最多.很好的一个条件是青蛙每次移动都是等间距的.题目需要注意将其排序. #i ...
- bzoj1930
一开始我觉得这不是一个弱弱的费用流吗? 每个豆豆拆点,入点出点随便连连 由于肯定是DAG图,边权为正的最大费用肯定能增广出来 于是我们只要跑总流量为2的最大费用最大流不就行了吗 但是 这样会TLE,因 ...
- Linux下搭建BT服务器
P2P(Peer to Peer 即对等网络)就是在这种背景下提出的一种网络技术,P2P可以简单地定义为通过直接交换信息,共享计算机资源和服务,对等计算机兼有客户机和服务器的功能.在这种网络中所有的节 ...
- 浏览器加载和渲染html的顺序-css渲染效率的探究
1.浏览器加载和渲染html的顺序1.IE下载的顺序是从上到下,渲染的顺序也是从上到下,下载和渲染是同时进行的.2.在渲染到页面的某一部分时,其上面的所有部分都已经下载完成(并不是说所有相关联的元素都 ...
- Sending data to USB printer in C#?
using System; using System.Drawing; using System.Drawing.Printing; using System.IO; using System.Run ...