码迷,mamicode.com
首页 > 其他好文 > 详细

75. Sort Colors

时间:2017-12-11 23:05:54      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:log   对象   相同   就是   span   spec   repr   sort   blog   

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.

给定一个带有红色,白色或蓝色的n个对象的数组,对它们进行排序,使相同颜色的对象相邻,颜色顺序为红色,白色和蓝色。在这里,我们将使用整数0,1和2分别表示红色,白色和蓝色。

(1)思想1:本质就是:对数组进行排序

 1 class Solution {
 2 public:
 3     void sortColors(vector<int>& nums) {
 4         for(int i=0;i<=nums.size();i++)
 5         {
 6             for(int j=i;j<nums.size();j++)
 7             {
 8                 if(nums[j]<nums[i])
 9                 {
10                     int sp=nums[i];
11                     nums[i]=nums[j];
12                     nums[j]=sp;
13                 }
14             }
15         }
16     }
17 };
18 //sort(nums.begin(),nums.end());

 

75. Sort Colors

标签:log   对象   相同   就是   span   spec   repr   sort   blog   

原文地址:http://www.cnblogs.com/sword-/p/8025337.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!