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 ...
随机推荐
- Memcached(四)Memcached的CAS协议
1. 什么是CAS协议很多中文的资料都不会告诉大家CAS的全称是什么,不过一定不要把CAS当作中国科学院(China Academy of Sciences)的缩写.Google.com一下,CAS是 ...
- selenium各种场景下的启动Firefox
开始学习selenium时为了启动Firefox可谓费尽周折,在大神的帮助下才堪堪搞定,走出了selenium的第一步:jdk1.8 + selenium_2.46 + Firefox国际版40.0. ...
- C语言预处理运算符
转自C语言预处理运算符 预处理还需要运算符?有没有搞错? ^_^, 没有搞错,预处理是有运算符,而且还不止一个: #(单井号) -- 字符串化运算符. ##(双井号 )-- 连接运算符 #@ ...
- MySQL学习笔记之一
MySQL装有一个名为mysql的命令行,在提示符下输入mysql将出现如下的简单提示: ➜ ~ mysql Welcome to the MySQL monitor. Commands end wi ...
- jquery data方法
jquery.data()文档:http://api.jquery.com/jQuery.data/ html5有个data-*属性,跟这个功能一样. Note: This is a low-leve ...
- Android 关于显示键盘,布局错乱网上顶的问题
<activity android:name="com.taiyi.DiscussActivity" android:windowSoftInputMode="st ...
- 请问什么是UTF字符串?
utf是编码方式,一般而言是国际性质的编码格式,有utf-8,utf-9,utf-16等多种形式,是最高级别的编码方式,也就是说如果你要读取的数据流设置成utf编码的话就要用到相应的编码方式来读取了, ...
- Oracle系列之包
涉及到表的处理请参看原表结构与数据 Oracle建表插数据等等 建包 -- 建立包头 create package mypkg is procedure set_ctx(p_name in varc ...
- 用Remastersys定制自己的Ubuntu安装光盘
这两天因为要做Ubuntu的平台移植,一直在给自己电脑上的Ubuntu系统装各种软件,其间几次将内核破坏,导致不得不重装系统,经过几次痛苦的等待,痛定思痛,决定试一试能不能将自己的Ubuntu系统定制 ...
- arcgis for android访问arcgis server上自己制作部署的地图服务
转自:http://gaomw.iteye.com/blog/1110437 本项目的开发环境是eclipse3.5 + ADT11插件+arcgis for andorid 插件 + arcgis ...