【Leet Code】 75.Sort Colors--medium
来源:互联网 发布:java的方法的重载 编辑:程序博客网 时间:2024/05/16 10:27
Given anarray with n objects colored red, white or blue, sort them so thatobjects of the same color are adjacent, with the colors in the order red, whiteand 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.
思路:
因为只有3种颜色,所以借助map结构,统计每种颜色的数量;
清空原来vector里面的内容;
按照map结构中统计的数据重新往vector里面加值。
class Solution {public: void sortColors(vector<int>& nums) { map<int, int> counts; counts.insert({0, 0}); counts.insert({1, 0}); counts.insert({2, 0}); for(auto item: nums) { switch(item) { case 0: ++counts[0]; break; case 1: ++counts[1]; break; case 2: ++counts[2]; break; } } nums.clear(); for(auto iter = counts.begin(); iter != counts.end(); ++ iter) { while(iter->second--) nums.push_back(iter->first); } return; }};
0 0
- 【Leet Code】 75.Sort Colors--medium
- [leet code] Sort Colors
- [Leet Code] 75. Sort Colors
- 【Leet Code】147. Insertion Sort List---Medium
- 【Leet Code】148. Sort List---Medium
- Medium 75题 Sort Colors
- LeetCode-75-Sort Colors(双指针)-Medium
- 【Medium】75. Sort Colors
- 75. Sort Colors 难度:medium
- CODE 58: Sort Colors
- [leet code] Insertion Sort List
- Leet Code Medium 1 two sum
- Leet Code Medium 2 add two numbers
- Leet Code Medium 15 3Sum
- Leet Code Medium 16 3Sum Closest
- Leet Code Medium 18 4Sum
- 【Leet Code】274. H-Index---Medium
- 【Leet Code】54. Spiral Matrix---Medium
- os引导程序boot从扇区拷贝os加载程序loader文件到内存(boot copy kernel to mem in the same method)
- Android 最火的快速开发框架XUtils
- poj2342—树形dp
- ubuntu mysql5.5 修改最大链接数
- 《机器学习》__note1
- 【Leet Code】 75.Sort Colors--medium
- eclipse 安装插件link方法
- Doxygen向导使用
- mysql group by 无数据补0 方法
- 调用摄像头和相册,从相册中选择(含有google发布的图片压缩以及自己的图片截取)
- hibernate核心开发接口
- cmd添加管理员
- tewsf
- 【Android技术整理】AsyncTask实现多线程处理