刷题75. Sort Colors

一、题目说明

题目75. Sort Colors,给定n个整数的列表(0代表red,1代表white,2代表blue),排序实现相同颜色在一起。难度是Medium。

二、我的解答

这个是一个排序,还是简单的,代码如下:

class Solution{
    public:
        void sortColors(vector<int>& nums){
            int num0=0,num1=0,num2=0;
            for(int i=0;i<nums.size();i++){
                if(nums[i]==0) num0++;
                if(nums[i]==1) num1++;
                if(nums[i]==2) num2++;
            }

            for(int j=0;j<nums.size();j++){
                if(j<num0) nums[j] = 0;
                else if(j<num0+num1) nums[j] = 1;
                else nums[j] = 2;
            }
        }
};

性能如下:

Runtime: 8 ms, faster than 10.95% of C++ online submissions for Sort Colors.
Memory Usage: 8.6 MB, less than 77.19% of C++ online submissions for Sort Colors.

三、优化措施

上述代码是2此遍历,其实只需要1此遍历,题目比较简单除此之外就不优化了:

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

            }
        }
};

性能如下:

Runtime: 0 ms, faster than 100.00% of C++ online submissions for Sort Colors.
Memory Usage: 8.7 MB, less than 77.19% of C++ online submissions for Sort Colors.

原文地址:https://www.cnblogs.com/siweihz/p/12250153.html

时间: 2024-10-23 22:03:58

刷题75. Sort Colors的相关文章

No.75 Sort Colors

No.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

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 bl

[leetcode 75] Sort Colors

1 题目 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, a

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 bl

75. Sort Colors (Array)

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 bl

[leetcode sort]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 bl

LeetCode 75 Sort Colors(颜色排序)

翻译 给定一个包括红色.白色.蓝色这三个颜色对象的数组.对它们进行排序以使同样的颜色变成相邻的,其顺序是红色.白色.蓝色. 在这里,我们将使用数字0.1和2分别来代表红色.白色和蓝色. 原文 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, whit

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 bl

75.Sort Colors(法1快排法2线性扫描统计赋值)

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 inthe order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red,white, and blue