Leetcode 75 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.

Note: You are not suppose to use the library's sort function for this problem.

```class Solution {
public:
void sortColors(vector<int>& nums) {
int a[3]={0};
for(int i=0;i<nums.size();i++) a[nums[i]]++;
nums.clear();
for(int i=0;i<3;i++)
for(int j=0;j<a[i];j++)
nums.push_back(i);
}
};```

0 条评论

• Leetcode 164 Maximum Gap 桶排序好题

Given an unsorted array, find the maximum difference between the successive ele...

• Leetcode 169 Majority Element

Given an array of size n, find the majority element. The majority element is th...

• Leetcode 239. Sliding Window Maximum

Given an array nums, there is a sliding window of size k which is moving from t...

• LeetCode 477 Total Hamming Distance

The Hamming distance between two integers is the number of positions at which th...

• LeetCode 169. Majority Element

思路：数组中有一个数字的出现次数超过一半，也就是说这个数字的出现次数比其他的所有的数字的出现次数之和还要多。因此我们可以考虑遍历数组的时候保存两个值，一个是数组...

• 十大经典排序算法整理汇总（附代码）

本文整理并总结了十大经典的排序算法（冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、计数排序、基数排序、桶排序、堆排序）的时间复杂度、空间复杂度等性...

• 全排列的两种实现方式(java)-poj2718

上述方法虽然能够实现全排列，但是方法的复杂度还是很高。指数级别增长。因为要遍历很多没用的情况。所以当数据较大并不能高速处理。所以换一种思路处理。 设[a,b,c...

• 【map超时】【set超时】1-5 两个有序序列的中位数 (25 分)

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。

• Leetcode 164 Maximum Gap 桶排序好题

Given an unsorted array, find the maximum difference between the successive ele...