Sort Colors问题及解法
来源:互联网 发布:idea如何新建java项目 编辑:程序博客网 时间:2024/05/18 00:54
问题描述:
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
问题分析:对于2,我们可以一直放数组右边,对于0,我们可以一直放数组左边。
过程详见代码:
class Solution {public: void sortColors(vector<int>& nums) { int right = nums.size() - 1;int left = 0;for (int i = 0; i <= right;i++){while (nums[i] == 2 && i < right) swap(nums[i], nums[right--]);while (nums[i] == 0 && left < i) {swap(nums[i], nums[left++]); }} }};
阅读全文
0 0
- Sort Colors问题及解法
- 【LeetCode】75. Sort Colors解法及注释
- Sort List问题及解法
- leetcode:Sort Colors 菜鸟解法
- Insertion Sort List问题及解法
- Sort Characters By Frequency问题及解法
- Leetcode之Sort Colors 问题
- [LeetCode] 荷兰国旗问题 Sort Colors
- LeetCode | Sort Colors(荷兰国旗问题)
- Sort Colors(荷兰旗问题)
- 75. Sort Colors--荷兰三色国旗问题
- Sort Colors
- sort colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- gd 添加freetype支持 。Call to undefined function Think\imagettftext() 错误解决
- C
- 2 3 5 7的倍数
- jersey上传文件例子
- js操作cookie,实现登录密码保存
- Sort Colors问题及解法
- JavaScript的六种继承方式
- MySQL中int(11)最大长度是多少?
- yii2,简单的留言板前段到后台
- Spark简介与计算模型
- 《机器学习实战》——在python中使用Matplotlib注解绘制树形图
- Swift UILabel显示添加中间划线
- 三个数从小到大排序
- lightoj-1370 Bi-shoe and Phi-shoe( 欧拉筛 求 欧拉函数)