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.
三个指针 lo =cur =0,hi =n-1
a[cur]==0: if(lo==cur) cur++ lo++
else: swap(lo,cur) lo++
class Solution {
public void sortColors(int[] nums) {
int begin = 0;
int cur = 0;
int end = nums.length-1;
while(cur<=end){
if(nums[cur]==2){
swap(nums,cur,end);
end--;
}
else if(nums[cur]==1){
cur++;
}
else //(nums[cur]==0)
{
if(nums[cur]!=nums[begin])
swap(nums,cur,begin);
begin++;
cur++;
}
}
}
private void swap(int[] nums,int i ,int j){
int temp;
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
75. Sort Colors(荷兰国旗问题 三指针)的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 75. Sort Colors - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【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 ...
- [leetcode]75. Sort Colors三色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- leetcode 75. Sort Colors (荷兰三色旗问题)
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- [LeetCode] 75. Sort Colors 颜色排序
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
随机推荐
- Oracle Tuning 总括
oracle tuning 分为3个阶段 1. application 调优阶段, 包括设计的调优, SQL语句调优, 管理权限等内容, (这部分是我的重点) (调优人员 application de ...
- jquery 改变checkbox的值
似乎没什么用... <script> $(document).ready(function(){ $("#comment").change(function(){ va ...
- 请说明meta标签的作用。
请说明meta标签的作用. 解答: meta是用来在HTML文档中模拟HTTP协议的响应头报文.meta 标签用于网页的<head>与</head>中,meta 标签的用处很多 ...
- 日历类Calendar
在早期的JDK版本中,日期(Date)类附有两大功能:(1)允许用年.月.日.时.分.秒来解释日期:(2)允许对表示日期的字符串进行格式化和句法分析.在JDK1.1中提供了类Calendar来完成第一 ...
- db2 sql
import from empty.del of replace into table_name
- WPF 附加属性的用法 (一)
public class MDCTest { public static DependencyProperty MouseDoubleClickCommandProperty = Dependency ...
- Xcode The operation couldn’t be completed. (NSURLErrorDomain error -1012.)
使用Xcode SVN 出现问题 The operation couldn’t be completed. (NSURLErrorDomain error -1012.) 解决方法: 打开终端 然后输 ...
- python 保存csv文件
利用pandas库, 将numpy的array数据保存成csv格式的文件: import pandas as pd import numpy as np data = pd.read_csv('C:\ ...
- AtCoder Tak and Hotels
题目链接:传送门 题目大意:有 n 个点排成一条直线,每次行动可以移动不超过 L 的距离,每次行动完成必须停在点上, 数据保证有解,有 m 组询问,问从 x 到 y 最少需要几次行动? 题目思路:倍增 ...
- bzoj2656
题目链接:传送门 题目大意:已知 a0=0:a1=1: n为偶数 an=a(n/2):n为基数 an=a(n/2)+a(n/2+1): 题目思路:因为n过大,所以要用java高精度,还有最多20组数据 ...