zoukankan      html  css  js  c++  java
  • 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.

    给定一个带有红色,白色或蓝色的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());
  • 相关阅读:
    day09
    day8
    day 7
    day 6
    PYTHON 学习
    day 5 作业
    day04作业
    Day03作业及默写
    python 2020 day4
    (copy)python操作excel
  • 原文地址:https://www.cnblogs.com/sword-/p/8025337.html
Copyright © 2011-2022 走看看