leetcode 75 Sorted Colors
两种解法
1)记录0和1的个数
然后按照记录的个数将0和1重新放入原数组,剩下的补2
2)双指针left,right
left表示0~left-1都为0,即i之前都为0
right表示right+1~nums.length都为2,即j之后都为2
遍历原数组
a)遇到为0的就把当前nums[i]与nums[left]交换
b)遇到为2的就交换nums[i]&nums[right],注意,写代码的时候要考虑交换过来的nums[right]有可能是2,如果i正常迭代变成i+1,漏掉了nums[right]为2的情况,所以我们这里i要减1
那么为什么前面a)不需要i减一呢?因为按照我们对left的定义,nums[left]不可能为0
class Solution {
public void sortColors(int[] nums) {
int len = nums.length;
int index=0, i=0, j=len-1;
while(index <= j){
if(nums[index] == 0){
nums[index] = nums[i];
nums[i++] = 0;
}
if(nums[index] == 2){
nums[index] = nums[j];
nums[j--] = 2;
index--;
}
index++;
}
}
}
leetcode 75 Sorted Colors的更多相关文章
- 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 so that objects of the same colo ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [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(颜色排序)
翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 Give ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- 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 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
随机推荐
- leetcode-157周赛-5214-最长定差子序列
题目描述: class Solution: def longestSubsequence(self, arr: List[int], difference: int) -> int: dp = ...
- 列表中的index,extend,count方法
列表中的index,extend,count方法 #_author:Administrator#date:2019/10/24#1.index方法l=['blue','red','white','bl ...
- js代码触发事件
/*** * 需要触发谁的点击事件 * @param how_id 节点的id 如:<input id='test'/> 则how_id=test * @param how_this 这个 ...
- 如何清除本机DNS缓存
如何清除本机DNS缓存 在实际应用过程中可能会遇到DNS解析错误的问题,就是说当我们访问一个域名时无法完成将其 解析到IP地址的工作,而直接输入网站IP却可以正常访问,这就是因为DNS解析出现故障造成 ...
- c++ const的用法
1.修饰成员变量 int value=0; int *p=&value; const int *p_c=&value;//指针指向常量,但是指针所指向的地址可以修改(int const ...
- Spring中的事件监听实现
在spring中我们可以自定义事件,并且可以使用ApplicationContext类型对象(就是spring容器container)来发布这个事件 事件发布之后,所有的ApplicaitonList ...
- springboot-配置多数据源之番外篇(分包实现)
场景: 随着业务发展,系统连接多数据库成为常态,继前面AOP的实现方式之后,这里记录一下分包实现的方式. 实现: 1.pom.xml <?xml version="1.0" ...
- 透视jvm之垃圾回收
JVM是JAVA世界的核心,了解它有助于我们更好调试,调优和开发程序,最近散仙在看JAVA特种兵一书,看完觉得,作者写的内容还是挺不错,大家感兴趣的,也可以购买本温故而知新下. 在JVM中,我们经常提 ...
- MFC入门--显示静态图片及调用本地软件
MFC是微软开发的基础类库,主要用来开发图形界面应用程序,在学习中,我们要验证算法好坏,一般需要对结果进行可视化. OpenCV是计算机视觉中的开源算法库,集成了很多先进算法,现在想将MFC与Open ...
- nginx 配置文件备份 nginx.conf and vhosts
bogon:vhosts xingchong$ brew services restart nginx Stopping `nginx`... (might take a while) ==> ...