LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place 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.
Example:
Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]
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 a one-pass algorithm using only constant space?
题目要求是,时间复杂度O(n) 空间复杂度O(1) 对于没有听过三路快排的窝来说,这个medium比hard难得多好趴。。。
/*
* @lc app=leetcode id=75 lang=javascript
*
* [75] Sort Colors
*/
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var sortColors = function(nums) {
let n = nums.length;
let lt = 0, // =v 的第一个
rt = n; // >v 的第一个 let index = 0;
let v = 1; while (index < n && index < rt) {
if (nums[index] > v) {
rt--;
swap(index, rt);
} else if (nums[index] === v) {
index++;
} else if (nums[index] < v) {
swap(lt, index);
lt++;
index++;
}
} function swap(i, j) {
let t = nums[i];
nums[i] = nums[j];
nums[j] = t;
}
};
LeetCode 75. Sort 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 so that objects of the same colo ...
- leetcode 75 Sort Colors 计数排序,三路快排
解法一:计数排序:统计0,1,2 的个数 时间复杂度:O(n) 空间复杂度:O(k) k为元素的取值范围, 此题为O(1) class Solution { public: void sortC ...
- LeetCode 75. Sort Colors (python一次遍历,模拟三路快排)
LeetCode 75. Sort Colors (python一次遍历,模拟三路快排) 题目分析: 本题需要实现数字只包含0,1,2的排序,并且要求一次遍历. 由于只用把数字隔离开,很容易想到快排的 ...
- 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 so that objects of the same colo ...
- 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 ...
随机推荐
- WPF 通过Win32SDK修改窗口样式
使用函数为 SetWindowLong GetWindowLong 注册函数 [DllImport("user32.dll", EntryPoint = "GetWind ...
- Logstash:运用jdbc_streaming来丰富我们的数据
需要学习的地方:使用logstash获取数据后,然后根据这些数据再从MySQL数据库中进行匹配,增加一些数据到logstash的数据流中,然后输出到es 在IoT物联网时代,我们经常会遇到从传感器采集 ...
- SAP-简单的OALV演示练习
接上一篇传统ALV:https://www.cnblogs.com/BruceKing/p/11320165.html. 首先介绍下什么是ALV,在R/3 4.6C之前ALV全称为ABAP List ...
- Devexpress treelist两张表父子节点设置、筛选、分页、排序、页面跳转demo
效果图 网上查了很多例子自己结合和修改了一下.最下方的分页跳转是dev的datapager控件.控件的属性事件自己研究一下. 代码如下 public partial class MMDefinitio ...
- 改变src图片不更新
var url = response + "?r" + Math.random();[完美解决修改src不更新图片]$('#loginimage').attr("src& ...
- asp.net实现页面跳转后不可以返回
window.history.go(0); Response.Write("<script> window.history.go(0);alert('恭喜user注册成功!!!\ ...
- [Tomcat源码分析] Eclipse中搭建Apache Tomcat源码调试环境
网上很多文章都推荐使用Ant下载编译,但本地实践中屡屡失败,无法下载. 后来参考 https://blog.csdn.net/xiongyouqiang/article/details/7894107 ...
- mvc 返回json格式时间格式化
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEnco ...
- Vim操作:打开文件
1.打开文件并定位到某一行 vim +20 vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php # 定位至第20行 2 ...
- PC端页面适应不同的分辨率的方法
原文链接:https://www.jianshu.com/p/4850a7b22228 一.根据不同的分辨率,加载不同的CSS样式文件 这个方法的思路是,分别针对800.1280.1440.1600. ...