一、题目说明

题目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. unity目前学的一些操作

    目前是根据b站的一位迈扣老师的30集基础教学学习的,用的是sunny land这个资源包进行的教学,这位老师讲得很清晰,吐词清晰,思路也清晰,推荐哦.其实我比较喜欢这样的老师,思路 吐词清晰.就像以前 ...

  2. Windows下Anaconda安装、换源与更新

    Anaconda指的是一个开源的Python发行版本,其包含了conda.Python等180多个科学包及其依赖项.当你尝试pip install xxx时出现各种意外和依赖问题,那么conda就是一 ...

  3. BFS和队列

    深度优先搜索(DFS)和广度优先搜索(BFS)是基本的暴力技术,常用于解决图.树的遍历问题. 首先考虑算法思路.以老鼠走迷宫为例: (1):一只老鼠走迷宫.它在每个路口都选择先走右边,直到碰壁无法继续 ...

  4. Java虚拟内存(栈、堆)

    一.java虚拟的内存可以分为几种 1. 第一种 栈(stack) 栈的特点 1.1 栈描述的是方法执行的内存模型,每个方法都被调用都会创建一个栈(存储局部变量.操作数. 方法出口等) 1.2 JVM ...

  5. 谷歌更新后,chromedriver如何更换新版本

    前天,更新了78版本的谷歌后,chromedriver便不能用了,于是在ChromeDriver仓库下载了相对应版本的chromedriver. 并且放入谷歌文件下C:\Program Files ( ...

  6. java文件分割及合并

    分割设置好分割数量,根据源文件大小来把数据散到子文件中代码如下; package word; import java.io.File; import java.io.FileInputStream; ...

  7. Jackson使用指南

    Jackson常用注解 序列化注解 @JsonAnyGetter 像普通属性一样序列化Map public class ExtendableBean { public String name; pri ...

  8. iptables (二) nat & tcp_wrapper

    一.nat 之前网络防火墙的示例中,如果内网是私网地址,那么内网主机如何与外网通信呢? 这时候,iptables要实现内网和外网通信,有两种方式: nat: Network Address Trans ...

  9. Pair类模板

    >Pair的实现是一个结构体而不是一个类< 1.标准头文件 #include<utility> 似乎无需引入该文件,在std命名空间内也有pair类型 2.格式为:templa ...

  10. 小希的迷宫 HDU - 1272

    #include<iostream> #include<algorithm> #include<cstring> using namespace std; cons ...