[leetcode-75-Sort Colors]

Posted hellowOOOrld

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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.

思路:

首先扫描一遍,统计不同元素个数。

类似于two pointers概念。

void sortColors(vector<int>& nums)
     {
         if (nums.size() <= 1)return;
         vector<int>temp = nums;
         int zero = 0, one = 0, two = 0, index0 = 0, index1 = 0, index2 = 0;
         for (int i = 0; i < nums.size();i++)
         {
             if (nums[i] == 0)zero++;
             if (nums[i] == 1)one++;
             if (nums[i] == 2)two++;             
         }
         index1 = zero, index2 = zero + one;
         for (int i = 0; i < temp.size(); i++)
         {
             if (temp[i] == 0)nums[index0++] = 0;
             if (temp[i] == 1)nums[index1++] = 1;
             if (temp[i] == 2)nums[index2++] = 2;
         }
     }

 





以上是关于[leetcode-75-Sort Colors]的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 75. Sort Colors

LeetCode 75 Sort Colors

LeetCode 75 Sort Colors

leetcode75 Sort Colors

LeetCode75 Sort Colors

Leetcode 75: Sort Colors