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 ...
随机推荐
- 快如闪电,触控优先。新一代的纯前端控件集 WijmoJS发布新版本了
全球最大的控件提供商葡萄城宣布,新一代纯前端控件 WijmoJS 发布2018 v1 版本,进一步增强产品功能,并支持在 Npm 上的安装和发布,极大的提升了产品的易用性. WijmoJS 是用 Ty ...
- python-转换函数使用
输入一个整数和进制,转换成十进制输出 输入格式: 在一行输入整数和进制 输出格式: 在一行十进制输出结果 输入样例: 在这里给出一组输入.例如: 45,8 输出样例: 在这里给出相应的输出.例如: 3 ...
- 虚拟机上 安装 CentoOS 7.5 1804 过程记录
1.准备安装镜像 在开始安装CentOS之前,必须下载安装ISO映像.镜像可从CentOS网站https://www.centos.org/download/.提供以下基本类型的镜像: DVD ISO ...
- python---从尾到头打印链表
class ListNode: def __init__(self, x): self.val = x self.next = None class Solution: # 返回从尾部到头部的列表值序 ...
- 论文解读(GRCCA)《 Graph Representation Learning via Contrasting Cluster Assignments》
论文信息 论文标题:Graph Representation Learning via Contrasting Cluster Assignments论文作者:Chun-Yang Zhang, Hon ...
- linux的时钟中断需要两个全局变量,分别是xtime与jiffies。
linux的时钟中断的两个内核全局变量,分别是xtime与jiffies. 1.xtime一个timeval结构类型变量,是从cmos电路(rtc)中取得的时间,一般是从某一历史时刻开始到现在的时间, ...
- linux部署项目访问mysql问题
springboot以war包形式传到webapps下面,mysql创建库和表,war包里配置数据源是localhost,然后运行tomcat是没有问题的,可以访问通mysql正常请求服务. ssm以 ...
- 使用IDEA生产JavaDoc文档
源代码 package com.*****.base; //文档注解 /** * @Author intelliyu * @version 1.0 //版本 * since 1.8 //指明需要最早使 ...
- 学习打卡——Mybatis—Plus
今天看完了Mybatis-Plus的视频,在某些方面来看MP确实简化了很多操作,比如自动生成代码等等.学习过程的代码实例也到同步到了gitee和github
- Java学习day8
今天学习了package,import,final,static和多态 package可以理解为文件夹,因为有些类可能重名,如果在同一个目录下就无法正常实现,所有需要有不同的包来装对应的类 Java出 ...