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 ...
随机推荐
- Chrome 手动安装.crx插件
在网上自己下载.crx的离线插件文件.或者通过朋友打包.crx文件.打包方式可参照Chrome 打包扩展程序 方法一: 打开Chrome浏览器,地址栏输入 chrome://extensions/ 将 ...
- AJAX发送异步请求教程详解
AJAX 一.AJAX简介 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML. AJAX 是一种用于创建快速动态网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可 ...
- jmeter入门操作 = 录制
没安装工具的朋友请先看:https://www.cnblogs.com/cainiaoxiansheng/p/11007754.html 软件装好,功能了解好,那么就开始进入正题,先来一波录制 不要鄙 ...
- 前端开发HTML5——函数
20.函数的调用方式(内容尚未完全,可自行百度这方面的知识) call().apply().bind() 这三个方法的作用都是改变执行环境中的this指针的指向 call()与apply() 这两 ...
- nginx缓存页面后,串会话问题的解决方案
用的Nigix 后面挂了二个Tomcat是springMVC session存在Redis的项目 但是上线以后反应A用户添加数据,变成B用户的,网上查的方案如下: 解决方案,nginx提供prox ...
- ansible服务部署
1.ansible.cfg配置文件 [defaults] #inventory= /home/op/ansible/testing #sudo_user=root remote_port=9122 r ...
- 6、Routing
Routing In the previous tutorial we built a simple logging system. We were able to broadcast log mes ...
- SVN Log命令常用组合【转】
转自:https://blog.csdn.net/xuanwenchao/article/details/8875103 版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请 ...
- HDU 1072 Nightmare 题解
Nightmare Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- Alpha2项目的测试
这个作业属于哪个课程 课程的链接 这个作业的要求在哪里 作业要求的链接 团队名称 西柚三剑客 这个作业的目标 -测试其他团队的作品,并进行体验总结 姓名 学号 团队名称 段小刚 20173106231 ...