leetcode刷题,总结,记录,备忘 75

原创 2015年07月08日 22:32:47

leetcode75

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.

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

比较简单的题目,但是后面有高的要求,先上简单版的

class Solution {
public:
    void sortColors(vector<int>& nums) {
        vector<int> temp;
        vector<int>::iterator it = nums.begin();
        while ((it = find(it, nums.end(), 0)) != nums.end())
        {
            temp.push_back(*it);
            it++;
        }
        it = nums.begin();
        while ((it = find(it, nums.end(), 1)) != nums.end())
        {
            temp.push_back(*it);
            it++;
        }
        it = nums.begin();
        while ((it = find(it, nums.end(), 2)) != nums.end())
        {
            temp.push_back(*it);
            it++;
        }
        nums = temp;
    }
};


更高级的要求是只有一次遍历,说实话我不会,,,,去查了别人的解法,有了理解。即将0扔到前面,将2扔到后面,如果是 1就不管,我看了别人的写法,,很容易的就理解了流程,但是其中有几个关键点,就是在先判断出是0,就是互换之后,下标不能自增,还应该再循环一次,再判断一下互换了之后的值是不是2,否则才 ++遍历下一个数。其实流程看几次就明白了,再不济就数字带进去试。我在初学变成的时候,看的郝斌的视频,他说过,如果有什么程序流程看不懂啥的,就带数进去试,试几次就懂了,的确是至理名言。

class Solution {
public:
    void sortColors(vector<int>& nums) {
       if (nums.size() <= 1)
       return;
       int s = -1, e = nums.size();
       int it = 0, temp;
       while (it < nums.size())
       {
           if (nums[it] == 0)
           {
               if (it > s)
               {
                   temp = nums[it];
                   nums[it] = nums[++s];
                   nums[s] = temp;
               }
               else it++;
           }else if  (nums[it] == 2)
           {
               if (it < e)
               {
                   temp = nums[it];
                   nums[it] = nums[--e];
                   nums[e] = temp;
               }
               else
               it++;
           }
           else
           it++;
       }
       
    }
};



版权声明:本文为博主原创文章,未经博主允许不得转载。

leetcode刷题,总结,记录,备忘 201

leetcode201Bitwise AND of Numbers Range Given a range [m, n] where 0 For example, given the rang...

leetcode刷题,总结,记录,备忘 223

leetcode223 Find the total area covered by two rectilinear rectangles in a 2D plane. Each re...

leetcode刷题, 总结,记录, 备忘142

leetcode142 Given a linked list, return the node where the cycle begins. If there is no cycle, retu...

leetcode刷题,总结,记录,备忘 313

leetcode313Super Ugly Number Write a program to find the nth super ugly number. Super ugly n...

leetcode刷题,总结,记录,备忘300

leetcode300,Longest Increasing Subsequence Given an unsorted array of integers, find the length...

leetcode刷题,总结,记录,备忘 312

leetcode312Burst Balloons Given n balloons, indexed from 0 to n-1. Each balloon is painted with a...

leetcode刷题,总结,记录,备忘 39

Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher...

leetcode刷题,总结,记录,备忘 88

leetcode88,Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nu...

leetcode刷题,总结,记录,备忘 19

leetcode19Remove Nth Node From End of List Given a linked list, remove the nth node from the en...

leetcode刷题,总结,记录,备忘 8

leetcode8 String to Integer (atoi)   Implement atoi to convert a string to an integer. Hint: Ca...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode刷题,总结,记录,备忘 75
举报原因:
原因补充:

(最多只允许输入30个字)