Leetcode75. Sort Colors颜色分类
给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。
此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。
注意:
不能使用代码库中的排序函数来解决这道题。
示例:
输入: [2,0,2,1,1,0] 输出: [0,0,1,1,2,2]
进阶:
- 一个直观的解决方案是使用计数排序的两趟扫描算法。
首先,迭代计算出0、1 和 2 元素的个数,然后按照0、1、2的排序,重写当前数组。
- 你能想出一个仅使用常数空间的一趟扫描算法吗?
双指针
class Solution {
public:
void sortColors(vector<int>& nums)
{
int len = nums.size();
int low = 0;
int high = len - 1;
int i = 0;
while(i <= high)
{
if(nums[i] == 0)
{
int temp = nums[low];
nums[low] = nums[i];
nums[i] = temp;
i++;
low++;
}
else if(nums[i] == 1)
{
i++;
}
else if(nums[i] == 2)
{
int temp = nums[high];
nums[high] = nums[i];
nums[i] = temp;
high--;
}
}
}
};
Leetcode75. Sort Colors颜色分类的更多相关文章
- 75. Sort Colors(颜色排序) from LeetCode
75. Sort Colors 给定一个具有红色,白色或蓝色的n个对象的数组,将它们就地 排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色. 这里,我们将使用整数0,1和2分别表示红色, ...
- [LeetCode] 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 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 ...
- LeetCode75 Sort Colors
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- LeetCode 75. 颜色分类(Sort Colors) 30
75. 颜色分类 75. Sort Colors 题目描述 给定一个包含红色.白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色.白色.蓝色顺序排列. 此题中, ...
- LeetCode 75. Sort Colors (颜色分类):三路快排
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the ...
- Java for LintCode 颜色分类
给定一个包含红,白,蓝且长度为n的数组,将数组元素进行分类使相同颜色的元素相邻,并按照红.白.蓝的顺序进行排序. 我们可以使用整数0,1和2分别代表红,白,蓝. 解题思路: Java for Leet ...
- Lintcode: Sort Colors II
Given an array of n objects with k different colors (numbered from 1 to k), sort them so that object ...
随机推荐
- vue-router配置子路由
1.改写App.vue 里面的代码 ,增加路由跳转,增加Hi页面1,Hi页面2的跳转 2.修改HI.vue 里面的内容,增加 <router-view class="aaa" ...
- Android开发 QRCode二维码开发第三方框架
前言 Android开发里二维码开发经常用到,这里简单的介绍下Android开发里的二维码. 最广泛使用的二维码库zxing zxing是最广泛的二维码库各个平台都可以适用它,但是Android平台使 ...
- openwrt MySQL移植
1 选择包 选择两个包,拷贝配置文件 cp products/mt7621/config_6080 .config 编译固件 openwrt 百万数据的优化, 执行脚本: mysql -u root ...
- 关于 wpf 的ICommand 的 CanExecute CanExecuteChanged func action的认识
关于 wpf 的ICommand 的 CanExecute CanExecuteChanged func action的认识
- for循环总结
1.冒泡排序的总结:其实这个很简单的理解,就是用数组里面的第0个元素(也就是[]里面的第一个数,按照数组的话是第一个,让他们进行挨个比较),示例: 排序前: 14 62 38 41 53 62 71 ...
- 初步了解Redis
参考: https://juejin.im/post/5b4dd82ee51d451925629622?utm_source=gold_browser_extension https://www.cn ...
- vue导航条选中项样式
html: <div id="app"> <div class="collection"> <a href="#!&qu ...
- Amazon DynamoDB
- python 请求测试环境的https接口地址报SSL错误验证,访问不了
解决文案: response = requests.post(url, data=payload, json=None, headers=headers,verify=False)print(resp ...
- css---3链接伪类与动态伪类
链接伪类link:表示作为超链接,并指向一个未访问的地址的所有锚 链接伪类不可以加在div上 <!DOCTYPE html> <html> <head> <m ...