75. Sort Colors
来源:互联网 发布:公交线路查询软件 编辑:程序博客网 时间:2024/06/01 22:48
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.
click to show follow up.
Subscribe to see which companies asked this question
准备两个指针 一个指向开头的最后一个0 一个指向末尾的第一个2 然后在数组中搜索 0放前面2放后面
public class Solution { public void sortColors(int[] nums) { int begin = 0; int end = nums.length-1; int p = 0; while(p<=end){ if(nums[p]==0){ int a = nums[begin]; nums[begin]=nums[p]; nums[p]=a; p++;begin++; continue; } if(nums[p]==2){ int a = nums[end]; nums[end]=nums[p]; nums[p]=a; // p++; end--; continue; } p++; } // return nums; }}
0 0
- 【sort专题】75. Sort Colors
- 75. Sort Colors
- [LeetCode]75.Sort Colors
- LeetCode --- 75. Sort Colors
- [Leetcode] 75. Sort Colors
- [leetcode] 75.Sort Colors
- 75. Sort Colors
- 【leetcode】75. Sort Colors
- 75. Sort Colors
- [leetcode] 75. Sort Colors
- Leetcode 75. Sort Colors
- *LeetCode 75. Sort Colors
- 75. Sort Colors LeetCode
- 75. Sort Colors
- Leetcode 75. Sort Colors
- 75. Sort Colors
- [LeetCode]75. Sort Colors
- 75. Sort Colors
- asp.net将图片上传并存入Sql…
- arclist标签在文章页列表页调用所有(全站)栏目最新文章热门文章的方法 [复制链接]
- jQuery-innerfade内部列表自动淡入…
- DedeCMS让{dede:list}标签支持weight权重排序、flag属性
- JQuery
- 75. Sort Colors
- oracle 都是parallel惹的祸【1-2分钟出结果变1-2秒】
- dede list 2个不同栏目如何调用同一个栏目文章,类似副栏目?
- 自定义函数中返回表的例子
- PHP错误报告等级设置 ,不再着ERROR的道
- 框架:让页面等待几秒跳到另一个页…
- 判断是否存在某表,如果存在删除重…
- ecshop促销时间倒计时效果
- Dom对象和jQuery包装集