刷题75. Sort Colors
一、题目说明
题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起。难度是Medium。
二、我的解答
这个是一个排序,还是简单的,代码如下:
class Solution{
public:
void sortColors(vector<int>& nums){
int num0=0,num1=0,num2=0;
for(int i=0;i<nums.size();i++){
if(nums[i]==0) num0++;
if(nums[i]==1) num1++;
if(nums[i]==2) num2++;
}
for(int j=0;j<nums.size();j++){
if(j<num0) nums[j] = 0;
else if(j<num0+num1) nums[j] = 1;
else nums[j] = 2;
}
}
};
性能如下:
Runtime: 8 ms, faster than 10.95% of C++ online submissions for Sort Colors.
Memory Usage: 8.6 MB, less than 77.19% of C++ online submissions for Sort Colors.
三、优化措施
上述代码是2此遍历,其实只需要1此遍历,题目比较简单除此之外就不优化了:
class Solution{
public:
void sortColors(vector<int>& nums){
int left=0,right=nums.size()-1;
while(left<right){
while(left<right && nums[left]==0){
left++;
}
while(left<right && nums[right]==2){
right--;
}
if(left<right && nums[left]==2 && nums[right]==0){
swap(nums[left],nums[right]);
left++;
right--;
}else{
for(int t=left+1;t<=right;t++){
if(nums[t]==0){
swap(nums[left],nums[t]);
left++;
break;
}else if(nums[t]==2){
swap(nums[t],nums[right]);
right--;
break;
}else if(t==right){
swap(nums[right],nums[left]);
left++;
right--;
break;
}
}
}
}
}
};
性能如下:
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Sort Colors.
Memory Usage: 8.7 MB, less than 77.19% of C++ online submissions for Sort Colors.
刷题75. Sort Colors的更多相关文章
- [刷题] 75 Sort Colors
要求 给只有0 1 2三个元素的数组排序 思路 方法1:遍历数组,利用辅助数组保存三个元素的个数,再写入(遍历两遍) 辅助数组有三个元素,对应0 1 2的个数 方法2:模拟三路快排,遍历一遍完成排序 ...
- 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 - LeetCode
Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...
- 【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 ...
- 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 ...
- leetCode 75.Sort Colors (颜色排序) 解题思路和方法
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
随机推荐
- 社会工程学 | 浅谈“答题”APP的赌博骗局
本文写于2018年2月18日,转载于我的知乎文章,请结合实际阅读. 这么多年来在认识的网骗手段越来越多了,考虑到今后工作方向,会继续记录. 微信小程序"头脑王者"被封禁后,更多 ...
- 以下几种情况转换成布尔类型会得到false
0 -0 '' NaN undefined null false document.all()
- linq to sql 比较字符串形式的时间
where Convert.ToDateTime(t.Date.Trim()).CompareTo(Convert.ToDateTime("2009/9/9")) >= 0 ...
- hibernate报错:MappingException: Could not determine type for...解决办法
有时候实体里的一些属性并不想映射到数据库(比方说子级菜单List), 如果不做处理的话会报字段映射错误找不到这列Column Not Found 例如:org.hibernate.MappingExc ...
- scrapy(一)--Pycharm创建scrapy项目
1.环境 操作系统:windows10. python版本:python3.6,Anaconda(将Anaconda3\Scripts;路径添加到环境变量Path中) pycharm:pycharm2 ...
- “石家庄铁道大学软件工程系学生学籍管理系统2019版”java小程序制作分享
首先附上完整代码: import java.util.Scanner; public class SocreInformation { public SocreInformation(){}; pub ...
- Tomcat开启JMX监控
搭建模拟环境: 操作系统:centos7内存:1Gjdk:1.8.0_131tomcat:8.0.48 环境准备我们这里就不直接演示了,直接配置tomcat的jmx 1.进入到tomcat的bin目录 ...
- Spark学习之路 (十八)SparkSQL简单使用[转]
SparkSQL的进化之路 1.0以前: Shark 1.1.x开始: SparkSQL(只是测试性的) SQL 1.3.x: SparkSQL(正式版本)+Dataframe 1.5.x: Spar ...
- JS:javascript 函数后面有多个小括号是怎么回事?f( )( )( )...
有时我们看见js函数后面跟着多个小括号是怎么回事?f( )( )( )... f()意思是执行f函数,返回子函数 f()()执行子函数,返回孙函数 f()()()执行孙函数 ()()表示定义并执行,使 ...
- C语言 typedef struct _STUDENT {}STUDENT,*PSTUDENT;
#include <stdio.h> #include <stdlib.h> #include <string.h> //给stuct _STUDENT 起一个别名 ...