刷题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 ...
随机推荐
- TCP与UDP的一些心得
1:CC攻击是正常的业务逻辑,大并发让你处理不过来,处理XP SP2,以上的系统都封了RAW格式协议封包自定义,除了基于应用层改协议,之外都是模拟或请求来测试传输层2:UDP不会粘包,不会少包,除非缓 ...
- ES6基础与解构赋值(高颜值弹框小案例!)
let只作用在当前块级作用域内使用let或者const声明的变量,不能再被重新声明let不存在`变量提升` console.log(dad); var dad = '我是爸爸!';//预定义undef ...
- 安卓平台SQLite数据库基础操作总结
最近学了一些安卓开发,在这里分享一下SQLite数据库的使用相关部分,我使用的工具为Android Studio,后台语言为java: 首先,需要创建一个数据库辅助类DataBaseHelper,用于 ...
- 剑指offer-面试题20-表示数值的字符串-字符串
/* 题目: 判断字符串是否表示数值. */ /* 思路: 字符串遵循模式A[.[B]][e|EC] ,[+|-].B[e|EC] A.C为可能带正负号的数字串 B为数字串 */ #include&l ...
- 剑指offer-面试题19-正则表达式匹配-字符串
/* 题目: 实现一个函数用来匹配包含'.'和'*'的正则表达式. '.'表示比配任意字符,‘*’表示匹配0个或多个字符串. */ /* 思路: 采用递归的方法. 基础条件:当字符串和模式串存在空的情 ...
- webkit 技术内幕 笔记 二
浏览器历史 80年代末-90年代初:worldwideweb(nexus) -- Berners-Lee 1993: Mosaic浏览器,后来叫网景(Netscape)--Marc Andreesse ...
- cf1266D
注意到每一个的点出入流是不会变的,因此本质是让构造一张图满足这个出入流并且边上的流量之和最少,显然流量是平衡的,也就是所有节点的出入流之和为0 因此我们可以直接暴力的选择让负数点向正数点连边,连之后就 ...
- 为什么重写equals方法,还必须要重写hashcode方法
一.equals方法和hashcode的关系 根据Object.hashCode的通用约定: 如果两个对象相同(equals方法返回true),那么hashcode也相等.(图1) 如果两个对象的ha ...
- ubuntu18.04 安装与卸载 php7.2
安装: 如果之前有其他版本PHP,在这边禁用掉 1 sudo a2dismod php5 再来安装做准备 1234 sudo apt-get install software-properties-c ...
- vue动态绑定图片和背景图
1.动态绑定图片 <img class="binding-img" :src="require('../assets/images/test.png')" ...