一、题目说明

题目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的更多相关文章

  1. [刷题] 75 Sort Colors

    要求 给只有0 1 2三个元素的数组排序 思路 方法1:遍历数组,利用辅助数组保存三个元素的个数,再写入(遍历两遍) 辅助数组有三个元素,对应0 1 2的个数 方法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 ...

  3. 75. Sort Colors(颜色排序) from LeetCode

      75. Sort Colors   给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...

  4. 75. Sort Colors - LeetCode

    Question 75. Sort Colors Solution 题目大意: 给一个数组排序,这个数组只有0,1,2三个元素,要求只遍历一遍 思路: 记两个索引,lowIdx初始值为0,highId ...

  5. 【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 ...

  6. [LeetCode] 75. Sort Colors 颜色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  7. 75. Sort Colors(中等)

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  8. [leetcode]75. Sort Colors三色排序

    Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...

  9. leetCode 75.Sort Colors (颜色排序) 解题思路和方法

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

随机推荐

  1. honeywell1900扫描枪的使用说明

    霍尼韦尔1900扫描枪驱动是honeywell1900扫描枪的USB驱动,就是扫描枪usb转com,如果你的系统是32位的,就直接运行Install_x86.bat,如果是64位的,就运行Instal ...

  2. Luogu1738 | 洛谷的文件夹 (Trie+STL)

    题目描述 kkksc03是个非凡的空想家!在短时间内他设想了大量网页,然后总是交给可怜的lzn去实现. 洛谷的网页端,有很多文件夹,文件夹还套着文件夹. 例如:\(/luogu/application ...

  3. JAVA成长之路SpringCloud脚印(一)

    从即日起开始学习SpringCloud,在这里记录下学习过程,共勉,欢迎指正. 环境:IDEA2019.3.3.JAVA13 一.spring cloud简介 spring cloud 为开发人员提供 ...

  4. JavaSE学习笔记(7)---数组

    JavaSE学习笔记(7)---数组 1.什么是数组 数组是相同类型数据的有序集合.数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成.其中,每一个数据称作一个元素,每个元素可以通过一个 ...

  5. HTML连载65-过渡模块的基本使用

    一.过渡模块的基本使用 1.*:hover;这个伪类选择器除了可以用在a标签上,还可以用在其他任何标签上. 2.过渡三要素: (1)必须要有属性发生变化:(2)必须告诉系统哪个属性需要执行过渡效果:( ...

  6. Yarn报错:Exception message: /bin/bash: line 0: fg: no job control

    Exception message: /bin/bash: line 0: fg: no job control 这个错误是 本地idea跨平台远程调试hadoop集群出现的,在使用windows调用 ...

  7. 【巨杉数据库SequoiaDB】巨杉数据库 v5.0 Beta版 正式发布

    2020年疫情的出现对众多企业运营造成了严重的影响.面对突发状况,巨杉利用长期积累的远程研发协作体系,仍然坚持进行技术创新,按照已有规划­­推进研发工作,正式推出了巨杉数据库(SequoiaDB) v ...

  8. ubuntu---禁止更新内核

    系统内核 4.15.0-29  更新成了 4.15.0-88,降级内核并禁止更新内核 查看已安装内核:dpkg --get-selections |grep linux-image 查看正在使用的内核 ...

  9. linux command - 在所有递归子文件夹中的py文件中,搜索包含指定字符串的文件

    find . -type f -iname "*.py" -exec grep -Hi "LAD" '{}' +

  10. AE开发中添加EngineOrDesktop后仍然有错误

    .AO是32位原生组件,一般认为不支持64位系统(道听途说),所以只能在32位环境下进行编译. 在配置管理器中,新建x86后问题解决了