首页 > 编程语言 >LeetCode_Array_75. Sort Colors 颜色分类 (C++)

LeetCode_Array_75. Sort Colors 颜色分类 (C++)

时间:2022-10-27 16:06:58浏览次数:44  
标签:Sort sort right nums int mid C++ Colors swap


目录

​1,题目描述​

​2,思路​

​3,代码【C++】​


1,题目描述

Given an array with n objects colored red, white or blue, sort them in-place 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.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Follow up:

    A rather straight forward solution is a two-pass algorithm using counting sort.
    First, iterate the array counting number of 0's, 1's, and 2's, then overwrite array with total number of 0's, then 1's and followed by 2's.
    Could you come up with a one-pass algorithm using only constant space(常数空间)?

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sort-colors
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

2,思路

emmm,有图有真相,就不多说了。。。

时间复杂度O(n)

LeetCode_Array_75. Sort Colors 颜色分类 (C++)_时间复杂度

LeetCode_Array_75. Sort Colors 颜色分类 (C++)_c++_02

最后

LeetCode_Array_75. Sort Colors 颜色分类 (C++)_时间复杂度_03

3,代码【C++】

 

class Solution {
public:
void sortColors(vector<int>& nums) {
int left = 0, mid = 0, right = nums.size() - 1;
while(mid <= right){
if(nums[mid] == 0){
swap(nums, left, mid);
left++;mid++;
}
else if(nums[mid] == 1){
mid++;
}
else{
swap(nums, mid, right);
right--;
}
}
}

void swap(vector<int>& nums, int i, int j){
int temp ;
temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
};

 

标签:Sort,sort,right,nums,int,mid,C++,Colors,swap
From: https://blog.51cto.com/u_15849465/5801371

相关文章

  • LeetCode_Array_73. Set Matrix Zeroes (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxnmatrix,ifanelementis0,setitsentirerowandcolumnto0.Do......
  • LeetCode_Array_64. Minimum Path Sum (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码【C++】​​​​4,测试效果​​1,题目描述Givenamxngridfilledwithnon-negativenumbers,findapathfromtopleftt......
  • LeetCode_Array_63. Unique Paths II (C++)
    目录​​1,题目描述​​​​2,思路​​​​3,代码​​​​4,测试结果​​1,题目描述Arobotislocatedatthetop-leftcornerofamxngrid(marked'Start'inthediagra......
  • LeetCode_Array_56. Merge Intervals (C++)
    目录​​1,题目描述​​​​2,解题思路​​​​3,代码【C++】​​​​4,运行比较​​1,题目描述Givenacollectionofintervals,mergealloverlappingintervals.Example1:I......
  • LeetCode_Array_62. Unique Paths (C++)
    目录​​1,题目描述​​​​2,思路​​​​思路一:排列组合​​​​思路二:动态规划​​​​方法一:空间复杂度O(m*n)​​​​方法二:空间复杂度O(2n)​​​​方法三:空间复杂度O(n......
  • LeetCode_Array_55. Jump Game (C++)
    目录​​1,题目描述​​​​2,解题思路​​​​3,代码【C++】​​​​4,运行结果​​1,题目描述Givenanarrayofnon-negativeintegers,youareinitiallypositionedatthe......
  • PAT_甲级_1004 Counting Leaves (30分) (C++)
    目录​​1,题目描述​​​​题目大意​​​​输入:​​​​输出:​​​​2,解题思路​​​​关键:​​​​存储结构:​​​​dfs算法实现:​​​​3,代码【C++】​​1,题目描述 Samp......
  • PAT_甲级_1035 Password (20分) (C++)【字符串处理/签到题】
    目录​​1,题目描述​​​​2,思路​​​​3,代码​​1,题目描述  2,思路将字符串中指定字符替换为其他字符。直接看代码吧。。。字符串处理参考了这篇文章​​@小明他很忙【C......
  • *PAT_甲级_1033 To Fill or Not to Fill (25分) (C++)【贪心算法】
    目录​​1,题目描述​​​​题目大意​​​​输入​​​​输出​​​​说明​​​​2,思路​​​​数据结构​​​​算法​​​​3,代码​​1,题目描述SampleInput1:5013001......
  • PAT_甲级_1031 Hello World for U (20分) (C++)【数学推理/字符串处理】
    目录​​1,题目描述​​​​题目大意​​​​2,思路​​​​3,代码​​1,题目描述SampleInput:helloworld! SampleOutput:h!edlllowor题目大意给定一个字符串,将其所......