75. Sort Colors - LeetCode
Question

Solution
题目大意:
给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍
思路:
记两个索引,lowIdx初始值为0,highIdx初始值为nums.length - 1,遍历数组,如果是2就与highIdx处元素互换且highIdx—,如果是0就与lowIdx处元素互换且lowIdx++,i++,还剩一种情况就是1了,执行i++,循环结束条件是i<=highIdx
Java实现:
public void sortColors(int[] nums) {
int lowIdx = 0;
int highIdx = nums.length - 1;
int i = 0;
while (i <= highIdx) {
if (nums[i] == 2) {
int tmp = nums[highIdx];
nums[highIdx--] = nums[i];
nums[i] = tmp;
} else if (nums[i] == 0) {
int tmp = nums[lowIdx];
nums[lowIdx++] = nums[i];
nums[i++] = tmp;
} else {
i++;
}
}
}
75. Sort Colors - LeetCode的更多相关文章
- 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(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- 刷题75. Sort Colors
一.题目说明 题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起.难度是Medium. 二.我的解答 这个是一个排序,还是 ...
- 【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 so that objects of the same colo ...
- 【一天一道LeetCode】#75. Sort Colors
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】75. Sort Colors 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 计数排序 双指针 日期 题目地址:https://l ...
- Leetcode 75. Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- idea问题之"一个或多个listeners启动失败问题"
org.apache.catalina.core.StandardContext.startInternal 一个或多个listeners启动失败,更多详细信息查看对应的容器日志文件之 "A ...
- hackgame2018_签到
hackgame2018_签到 进入题目得到如下提示 尝试提交 发现这个输入框长度做了限制我们将前端js修改一下 然后提交以下这样就发现了flag这题比较简单--
- Altium design16设计技巧
第一栏:共有界面 1.在原理图和PCB都打开的情况下,选中原理图可以对应到PCB界面元件里面 第二栏:原理图界面 1.批量改变元件属性 选择某一元件-查找相似对象-将其要改变的内容设置为same-点击 ...
- (Math.round(num*100)/100).toFixed(2); 将输入的数字变成保留两位小数
<input type="number" @input="onInputPrice" @blur="onPrice" data-id= ...
- canvas元素内容生成图像文件
准备工作 想要将canvas元素当前显示的内容生成为图像文件,我们首先要获取canvas中的数据,在HTML5 <canvas>元素的标准中提供了toDataURL()的方法可以将canv ...
- 用Canvas画一棵二叉树
笔墨伺候 var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); // 然后便可以挥毫泼墨 ...
- 基于Vue实现关键词实时搜索高亮显示关键词
最近在做移动real-time-search于实时搜索和关键词高亮显示的功能,通过博客的方式总结一下,同时希望能够帮助到别人~~~ 如果不喜欢看文字的朋友我写了一个demo方便已经上传到了github ...
- Override,Overload,Overwrite到底有什么区别?
Override,Overload,Overwrite的区别[新手可忽略不影响继续学习] 方法的覆盖(Override)是指子类重写从父类继承来的一个同名方法(参数.返回值也同),马克-to-win: ...
- LC-206
206. 反转链表 迭代法 class Solution { public ListNode reverseList(ListNode head) { //申请节点,pre和 cur,pre指向nul ...
- 20220406Java
记个笔记 字符串操作类中s1.compareTo(s)规则: Compares two strings lexicographically. The comparison is based on th ...