leetcode Array problem 905, 977, 830

扶醉桌前 提交于 2020-02-16 22:05:32

905. Sort Array By Parity

Given an array A of non-negative integers, return an array consisting of all the even elements of A, followed by all the odd elements of A.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

Time Limit Exceeded:

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        for(int i = 0; i < A.size(); )
        {
            if(A[i]%2 == 1)
            {
                A.push_back(A[i]);
                A.erase(A.begin()+i);
                
            } 
            else
                i++;
        }
        return A;
    }
};

Slow version:

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        vector<int> even;
        vector<int> odd;
        for(int i = 0; i < A.size();i++)
        {
            if(A[i]%2 == 1)
                odd.push_back(A[i]);
            else
                even.push_back(A[i]);
        }
        
        A.erase(A.begin(), A.end());
        
        A.insert(A.end(), even.begin(), even.end());
        A.insert(A.end(), odd.begin(), odd.end());
        return A;
    }
};

using Partition():

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        partition(A.begin(), A.end(), [](int i){return i%2==0;});
        return A;
    }
};

Performance:

Faster version using sort():

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
        sort(A.begin(), A.end(), [](int lhs, int rhs) {
        if (lhs % 2 == 1 && rhs % 2 == 0) {
            return false;      // left is odd, right is even, out of order
        } else if (rhs % 2 == 1 && lhs % 2 == 0) {
            return true;      // left is even, right is odd, OK
        } else {
            return false; 
        }
    });
        return A;
    }
};

 

Useful knowledge:

【C++】 vector.erase()

两个 vector 怎么合并?

C++ partition

977. Squares of a Sorted Array

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]
Output: [0,1,9,16,100]

Note:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. A is sorted in non-decreasing order.

Slow Version:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        for(int i = 0; i < A.size(); i++)
            A[i] = A[i]*A[i];
        sort(A.begin(), A.end());
        return A;
    }
};

Performance:

Two pointers:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& A) {
        int p1 = 0; int p2 = A.size()-1;
        vector<int> v(A.size());
        
        for(int i = A.size()-1; i >= 0; i--)
        {
            if(A[p1]<0 && abs(A[p1]) > A[p2])   //First error: A[p1] compares with A[p2] not with A[i]
            {
                v[i] = A[p1]*A[p1];
                p1++;
            }
            else
            {
                v[i] = A[p2]*A[p2];
                p2--;
            }
        }
        return v;
    }
};

Performance:

830. Positions of Large Groups

In a string S of lowercase letters, these letters form consecutive groups of the same character.

For example, a string like S = "abbxxxxzyy" has the groups "a""bb""xxxx""z" and "yy".

Call a group large if it has 3 or more characters.  We would like the starting and ending positions of every large group.

The final answer should be in lexicographic order.

Example 1:

Input: "abbxxxxzzy"
Output: [[3,6]]
Explanation: "xxxx" is the single large group with starting  3 and ending positions 6.

Note:  1 <= S.length <= 1000

Slow version:

class Solution {
public:
    vector<vector<int>> largeGroupPositions(string S) {
        vector<vector<int>> ans;
        
        int start = 0; int end = 0; 
        S = S + '#';   //in order to avoid S[i+1] being out of array index
        for(int i = 0; i < S.length()-1; i++)
        {
            if(end - start >= 2 && S[i] != S[i+1])
            {
                ans.push_back({start, end});
            }
            
            if(S[i]!=S[i+1])
            {
                start = end = i+1;
            }
            else
                end++;

        }
        return ans;
    }
};

Performance:

Useful knowledge:

C++ string类

 

 

 

 

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!