Leetcode 75. Sort Colors
来源:互联网 发布:php mongodb扩展 编辑:程序博客网 时间:2024/05/18 00:38
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.
排列数组中的0,1.2
代码
public class Solution { public void sortColors(int[] nums) { int length = nums.length; int notRed = 0; int notBlue = length - 1; while(notRed < length && nums[notRed] == 0) notRed++; while(notBlue >= 0 && nums[notBlue] == 2) notBlue--; int i = notRed; while(i <= notBlue){ int tem = nums[i]; if(tem == 0) { swap(nums,i,notRed); notRed++; i++; } else if(tem == 2){ swap(nums,i,notBlue); notBlue--; } else i++; } } public void swap(int[] nums, int a, int b){ int tem = nums[a]; nums[a] = nums[b]; nums[b] = tem; }}
0 0
- [LeetCode]75.Sort Colors
- LeetCode --- 75. Sort Colors
- [Leetcode] 75. Sort Colors
- [leetcode] 75.Sort Colors
- 【leetcode】75. Sort Colors
- [leetcode] 75. Sort Colors
- Leetcode 75. Sort Colors
- *LeetCode 75. Sort Colors
- 75. Sort Colors LeetCode
- Leetcode 75. Sort Colors
- [LeetCode]75. Sort Colors
- LeetCode 75. Sort Colors
- LeetCode *** 75. Sort Colors
- leetcode 75. Sort Colors
- LeetCode 75. Sort Colors
- leetcode-75. Sort Colors
- [leetcode]75. Sort Colors
- LeetCode-75.Sort Colors
- RW RO ZI ROM keil中的含义
- BZOJ 1415: [Noi2005]聪聪和可可|概率dp
- 2015年大二上-数据结构-图-1-(2)操作用邻接表存储的图
- webrtc
- menu.lst是什么?
- Leetcode 75. Sort Colors
- 【HDOJ 2255】奔小康赚大钱(KM算法)
- android之桌面悬浮框
- LeetCode 160 Intersection of Two Linked Lists(链表相交)(Linked List)(*)
- POJ 1837 二维背包 天平平衡问题
- SQL之模糊查询
- Codeforces Round #340 (Div. 2):A. Elephant
- <LeetCode OJ> 144 / 145 / 94 Binary Tree (Pre & In & Post) order Traversal
- CF 10C Digital Root