标签:计数排序 题目 etc style tor return color size 排序
这个题目超简单是不,用计数排序就搞定了~
1 class Solution { 2 public: 3 void sortColors(vector<int>& nums) { 4 if (nums.size() == 0) 5 { 6 return; 7 } 8 int count1 = 0, count0 = 0; 9 for (int num : nums) 10 { 11 if (num == 0) 12 { 13 ++count0; 14 } 15 if (num == 1) 16 { 17 ++count1; 18 } 19 } 20 int i = 0; 21 for (; i < count0; ++i) 22 { 23 nums[i] = 0; 24 } 25 for (; i < count0 + count1; ++i) 26 { 27 nums[i] = 1; 28 } 29 for (; i < nums.size(); ++i) 30 { 31 nums[i] = 2; 32 } 33 34 } 35 };
标签:计数排序 题目 etc style tor return color size 排序
原文地址:http://www.cnblogs.com/xiaoyajiang/p/7843506.html