Leetcode Sort Colors
来源:互联网 发布:批判性思维工具 知乎 编辑:程序博客网 时间:2024/06/06 03:58
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.
Difficulty: Easy
public class Solution { public void swap(int[] nums, int index1, int index2){ int temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = temp; return; } public void sortColors(int[] nums) { int curr = 0; for(int i = 0; i < nums.length; i++){ if(nums[i] == 0){ swap(nums, curr, i); curr++; } } for(int i = curr; i < nums.length; i++){ if(nums[i] == 1){ swap(nums, curr, i); curr++; } } }}
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
- 字节对齐详解
- 应用内跳转百度,高德地图
- 【题】【矩阵】NKOJ 1901 喜欢奇数的面包师
- java基础学习(5)疯狂java讲义第4章课后习题解答源码
- Codeforces Round #363 (Div. 2) [C] Vacations
- Leetcode Sort Colors
- java 多态,向上转型,向下转型(强制转换)之间的理解(学习笔记而已,不一定全部准确)
- Python下安装Numpy,Scipy,Matlotlib
- Activity四种启动模式
- 唐巧: 成长为 iOS 大 V 的秘密
- 最佳点割集
- 栈和队列面试题之--实现一个有getMin功能的栈
- 关于CAShapeLayer的一些实用案例和技巧
- 微信项目总结