Double pointer to solve array sorting problem

If you pay attention to this problem, you can solve it with one sentence

sort(nums.begin(),nums.end());

Done. But people clearly said that the sorting function in the code base cannot be used. We have to do the sorting ourselves. In fact, this problem is very simple, because there are only three colors in it, which are represented by 0 1 2. The idea is also very simple, that is, we traverse the array with double pointers. If you encounter 0, put it in the front, if you encounter 1, it will be in that position, and if you encounter 2, skip to the end. The code is as follows, I personally feel that it is particularly easy to use double pointers to solve the sorting problem of arrays.

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

Merging ranges can also be resolved with double pointers, one pointer to the current element and one pointer to the next element of the current element.

We first use the sort function to sort the array, and after sorting we can compare it more easily.

 

We can discuss this issue on a case-by-case basis. The first and simplest case is that there is no intersection such as [1,3][4,9]. That is, n[i][0]>c[i][1]

where n represents the next pointer and c represents the current pointer. At this time, the current element [1, 3] can be directly saved without further comparison. Just take [4,9] to compare with the latter.

In the second case [4,9][5,8] The front contains the back. Expressed in code, that is, n[i][1]<=c[i][1], don't care about [5, 8] at this time, because [5.8] is already included in [4, 9], take [4, 9] Continue to compare with the following numbers. That is, n++, c remain unchanged.

The last case is the case of overlapping, [4,9][7,10] This can be represented by code as long as 9 in zero [4,9] is equal to 10, c[i][1]=n [i][1] is enough, n=++ is done

paste code

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        if (intervals.size() == 0 || intervals.size() == 1) return intervals;
        int u = 0, v = 0;
        vector<vector<int>> ans;
        // 思路①
        sort(intervals.begin(), intervals.end());
        // 思路②
        while (v < intervals.size()) { 
            if (intervals[v][0] > intervals[u][1]) {
                ans.emplace_back(intervals[u]);
                u = v;
            } else if (intervals[v][1] <= intervals[u][1]) {
                v++;
            } else {
                intervals[u][1] = intervals[v][1];
                v++;
            }
        }
        //思路③
        ans.emplace_back(intervals[u]);
        return ans;
    }
};

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324187308&siteId=291194637