LeetCode 75. Sort Colors

来源:互联网 发布:苹果手机阅读软件 编辑:程序博客网 时间:2024/05/22 11:44

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];a[0]=0;a[1]=0;a[2]=0;        for(int i=0;i<nums.size();i++){            a[nums[i]]++;        }        cout<<a[0]<<" "<<a[1]<<" "<<a[2]<<endl;        int tot=0;        for(int i=0;i<a[0];i++){            nums[tot++]=0;        }        for(int i=0;i<a[1];i++){            nums[tot++]=1;        }        for(int i=0;i<a[2];i++){            nums[tot++]=2;        }            }};

原创粉丝点击