首页 > 学院 > 开发设计 > 正文

LeetCode 75. Sort Colors

2019-11-08 01:22:48
字体:
来源:转载
供稿:网友

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.

Note:

You are not suppose to use the library's sort function for this problem.

answer:

class Solution {public:    void sortColors(vector<int>& nums) {        int end0 = -1, end1 = -1, end2 = -1;        for(int i = 0; i < nums.size(); i ++){            if(nums[i] == 0){                nums[++end2] = 2;                nums[++end1] = 1;                nums[++end0] = 0;            }            else if(nums[i] == 1){                nums[++end2] = 2;                nums[++end1] = 1;            }            else nums[++end2] = 2;        }    }};


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表