[LeetCode]Sort Colors
来源:互联网 发布:mac屏幕截图保存位置 编辑:程序博客网 时间:2024/06/07 16:05
题目
Number: 75
Difficulty: Medium
Tags: Array, Two Pointers, Sort
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.
Note:
You are not suppose to use the library’s sort function for this problem.
题解
排序颜色。假设有红白蓝三色,分别用0、1、2表示,三色顺序打乱,要按照同色相邻的原则排序,不同色按照红白蓝排序。
代码
void sortColors(vector<int>& nums) { int i = 0, j = nums.size() - 1; for(int k = 0; k <=j;){ if(nums[k] == 0) swap(nums[k++], nums[i++]); else if(nums[k] == 2) swap(nums[k], nums[j--]); else k++; }}
0 0
- LeetCode: Sort Colors
- LeetCode Sort Colors
- LeetCode : Sort Colors
- leetcode 77: Sort Colors
- Leetcode 75 Sort Colors
- leetcode 101: Sort Colors
- [LeetCode]Sort Colors
- [leetcode]Sort Colors
- LeetCode-Sort Colors
- [leetcode] Sort Colors
- [LeetCode] Sort Colors
- [leetCode] Sort Colors
- LeetCode - Sort Colors
- leetcode:Sort Colors
- LeetCode | Sort Colors
- 【LeetCode】Sort Colors
- Leetcode: Sort Colors
- [LeetCode] Sort Colors
- [LeetCode]Search a 2D Matrix
- poj 3259 Wormholes <SPFA算法+求是否存在负环>
- 快排求第K大的数O(n)
- HDU 5416 CRB and Tree dfs
- ubuntu 12.04 KDE(Kool Desktop Environment,桌面环境)的安装
- [LeetCode]Sort Colors
- 闲话js前端框架(5)——再看自己一年前设计的微型渲染引擎
- 暴雪总裁总结游戏十条经验
- 抓住移动竞价蓝海,分享移动互联网SEM竞价干货
- [LeetCode]Combinations
- xcode7发送session请求的时候报错解决
- 算法初探——分治法
- HDOJ 1953 Bridging signals (STL,lower_bound)
- [LeetCode]Subsets