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 ...
随机推荐
- SQL语句之Column 'Status' in where clause is ambiguous错误
问题: AND created_by IN (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) limit 0, 10]; Column 'created_by' in where cla ...
- MOS管驱动电路,看这里就啥都懂了
一.MOS管驱动电路综述在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最大电流等,也有很多人仅仅考虑这些因素.这样的电路也许是可以工作的,但并不是优秀 ...
- mysql各个集群方案的优劣
集群的好处 高可用性:故障检测及迁移,多节点备份. 可伸缩性:新增数据库节点便利,方便扩容. 负载均衡:切换某服务访问某节点,分摊单个节点的数据库压力. 集群要考虑的风险 网络分裂:群集还可能由于网络 ...
- js判断输入数字是否是整数,金额、数字
function isIntNum(strNum){//js判断输入数字是否是整数 仅供学习思想 var strCheckNum = strNum+""; if(strCheckN ...
- 反射常用API以及内省机制(代码)
学习内容: (1)获取构造函数 这里不贴Person类了,不然代码太多太乱了,只给出一些常用API // 创建字节码对象 Class<?> aClass = Class.forName(& ...
- IO流的简单实现
IO流的几种实现方式 学习目标: 例题: 字节输出流 字节输入流 字符输入流 字符输出流 学习目标: 熟练掌握IO流的基本实现方式 例题: 字节输出流 代码如下: public class Outpu ...
- Ubuntu安装docker(摘自官网,自用)
在 Ubuntu 上安装 Docker 引擎(按照标红顺序执行命令) 预计阅读时间:11分钟 适用于 Linux 的 Docker 桌面 Docker Desktop 可帮助您在 Mac 和 Wind ...
- 如何在 Java 中实现无向图
基本概念 图的定义 一个图是由点集 \(V=\{v_i\}\) 和 \(V\) 中元素的无序对的一个集合 \(E=\{e_k\}\) 所构成的二元组,记为 \(G=(V,E)\),\(V\) 中的元素 ...
- 解决github上不去
github上不去 在hosts文件中加入下列IP,保存即可生效. !!!!!注意!!!!! 网站对应的IP需要去[https://www.ipaddress.com/]网站查询, 可能与下面给出的不 ...
- 启动分区查找可以通过 fdisk -l命令
这里有两个硬盘,一个硬盘有两个分区,sda1 的boot列 带*表示是启动分区,否则为空