【LeetCode OJ 075】Sort Colors
来源:互联网 发布:蜂窝移动数据开关无效 编辑:程序博客网 时间:2024/05/22 17:27
题目链接:https://leetcode.com/problems/sort-colors/
题目: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.
解题思路如下:遍历数组,存储每种颜色的个数,再进行排序,示例代码如下:
/** * @Description: * 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. * @author 徐剑 * @date 2016年3月27日 下午12:32:14 * @version V1.0 */public class Solution{ public static void sortColors(int[] nums) { if(nums==null||nums.length<=0) return; int a[]=new int[3]; for(int i=0;i<nums.length;i++) { a[nums[i]]++; } int x=0; int y=0; int z=0; for(int i=0;i<nums.length;) { while(x<a[0]) { x++; nums[i]=0; i++; continue; } while(y<a[1]) { y++; nums[i]=1; i++; continue; } while(z<a[2]) { z++; nums[i]=2; i++; continue; } } }}
0 0
- 【LeetCode OJ 075】Sort Colors
- LeetCode OJ:Sort Colors
- LeetCode OJ - Sort Colors
- [LeetCode OJ]Sort Colors
- LeetCode OJ Sort Colors
- [LeetCode OJ]75. Sort Colors
- LeetCode 075 Sort Colors
- LeetCode: Sort Colors [075]
- LeetCode 075 Sort Colors
- LeetCode 075 Sort Colors
- leetcode 075 Sort Colors
- LeetCode OJ平台上Sort Colors题目算法探讨
- LeetCode OJ算法题(七十五):Sort Colors
- LeetCode OJ 之 Sort Colors(颜色排序)
- LeetCode OJ 系列之75 Sort Colors --Python
- leetcode 075 —— Sort Colors
- LeetCode: Sort Colors
- LeetCode Sort Colors
- JS与WebView交互存在的一些问题
- [LeetCode]Combination Sum III
- EEPROM与FLASH
- xiaoxin juju needs help
- MFC-字体和颜色对话框
- 【LeetCode OJ 075】Sort Colors
- 自动装箱与自动拆箱的注意点
- FZU-2150-Fire Game【BFS】
- [BZOJ4134][JZOJ4401]ljw和lzr的hack比赛
- SQL(三)-- 练习题
- 文章标题
- JUnit片段
- hdu5652 India and China Origins(并查集)
- 如何编写Hadoop调度器