1. 程式人生 > >LeetCode Weekly Contest 118

LeetCode Weekly Contest 118

要死要死,第一題竟然錯誤8次,心態崩了呀,自己沒有考慮清楚,STL用的也不是很熟,一直犯錯。

第二題也是在室友的幫助下完成的,心態崩了。

970. Powerful Integers

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0

.

Return a list of all powerful integers that have value less than or equal to bound.

You may return the answer in any order.  In your answer, each value should occur at most once.

 

Example 1:

Input: x = 2, y = 3, bound = 10
Output: [2,3,4,5,7,9,10]
Explanation: 
2 = 2^0 + 3^0
3 = 2^1 + 3^0
4 = 2^0 + 3^1
5 = 2^1 + 3^1
7 = 2^2 + 3^1
9 = 2^3 + 3^0
10 = 2^0 + 3^2

Example 2:

Input: x = 3, y = 5, bound = 15
Output: [2,4,6,8,10,14]

 

Note:

  • 1 <= x <= 100
  • 1 <= y <= 100
  • 0 <= bound <= 10^6

題目意思很清楚就不解釋了。說下兩個很坑的資料吧,輸出結果可以任意順序, 第一個:輸入  1 1 1     輸出 [1]  第二個  1 2 100  輸出 [2,3,5,9,17,33,65]

class Solution {
public:
    vector<int> powerfulIntegers(int x, int y, int bound) {
        vector<int> ans;
        if( x < y ) swap(x, y);
        int px = 1, py = 1;
        while( px <= bound ) {
            py = 1;
            while( px + py <= bound ) {
                ans.push_back(px+py);
                py = py*y;
                if( y == 1 ) break;
            }
            px = px * x;
            if( x == 1 ) break;
        }
        sort(ans.begin(), ans.end());
        ans.erase(unique(ans.begin(), ans.end()), ans.end());
        return ans;
    }
};
View Code

 

969. Pancake Sorting

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first kelements of A.  We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.

Return the k-values corresponding to a sequence of pancake flips that sort A.  Any valid answer that sorts the array within 10 * A.lengthflips will be judged as correct.

 

Example 1:

Input: [3,2,4,1]
Output: [4,2,4,3]
Explanation: 
We perform 4 pancake flips, with k values 4, 2, 4, and 3.
Starting state: A = [3, 2, 4, 1]
After 1st flip (k=4): A = [1, 4, 2, 3]
After 2nd flip (k=2): A = [4, 1, 2, 3]
After 3rd flip (k=4): A = [3, 2, 1, 4]
After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted. 

Example 2:

Input: [1,2,3]
Output: []
Explanation: The input is already sorted, so there is no need to flip anything.
Note that other answers, such as [3, 3], would also be accepted.

 

Note:

  1. 1 <= A.length <= 100
  2. A[i] is a permutation of [1, 2, ..., A.length]

題意:規定了翻轉規則是:選擇一個數k將陣列前面的k個數字移到陣列後面。現在問你給你一個打亂順序的陣列A,你要儘量多少次這樣的翻轉才能將其變成一個有序的陣列(1-n排列)。

思路:無論怎麼翻轉,最後的結果肯定是A[i]=i+1,所以直接去陣列中找對應的數字然後直接按照規則翻轉就好。

class Solution {
public:
    vector<int> pancakeSort(vector<int>& A) {
        vector<int> ans;
        int n = A.size();

        for (int i = n; i > 0; i--) {
            int index = find(A.begin(), A.end(), i) - A.begin();
            ans.push_back(index + 1);
            reverse(A.begin(), A.begin() + index + 1);
            ans.push_back(i);
            reverse(A.begin(), A.begin() + i);
        }

        return ans;
    }
};
View Code