leetcode75: Sort Colors
来源:互联网 发布:怎么自己开淘宝网店 编辑:程序博客网 时间:2024/06/07 01:45
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.
public void sortColors(int[] nums) {int red = 0;int white = 0;int blue = 0;for (int i = 0; i < nums.length; i++)if (nums[i] == 0)red++;else if (nums[i] == 1)white++;else if (nums[i] == 2)blue++;int k = 0;while (k < red) {nums[k] = 0;k++;}while (k < red + white) {nums[k] = 1;k++;}while (k < red + white + blue) {nums[k] = 1;k++;}}
阅读全文
0 0
- Leetcode75 Sort Colors
- leetCode75:Sort Colors
- LeetCode75. Sort Colors
- leetcode75~Sort Colors
- leetCode75. Sort Colors
- leetcode75. Sort Colors
- leetcode75. Sort Colors
- leetcode75: Sort Colors
- LeetCode75 Sort Colors
- LeetCode75——Sort Colors
- Leetcode75 - Sort Colors(two pointers)
- LeetCode75(Sort Color)
- Sort Colors
- sort colors
- Sort Colors
- Sort Colors
- Sort Colors
- Sort Colors
- AngularJS系列之select下拉选择第一个选项为空白的解决办法
- 【C】初学习之库函数——通用的实用工具<stdlib.h>之伪随机序列生成函数
- Http与网络通信(GET和POST方式)
- 求最短路即次短路模板,一条边可以重复走的HDU6181
- [笔记分享] [OS] Linux的时间管理
- leetcode75: Sort Colors
- R语言对高频交易订单流进行建模分析 1
- weex 学习历程
- 报错`mobx.map` is deprecated, use `new ObservableMap` or `mobx.observable.map`instead
- 1015:大小写问题
- var 与let const比较
- mybatis怎么返回某列的最大值
- 数据结构-图的应用之最短路径
- matplotlib.patches.Polygon