leetcode轮回计划20180923

  1. 53 Maximum Subarray
    题意:子数组的最大和
    思路:简单的动态规划
  2. 54 Spiral Matrix
    题意:要求按照螺旋的方式遍历矩阵
    思路:按照最简单的方式来遍历就好了
  3. 55 Jump Game
    题意:跳台阶,每个数字标识可以跳到的最远的位置,判断是否能跳到最后一个台阶
    思路:简单的动态规划
  4. 56 Merge Intervals
    题意:合并可以合并的pair
    思路:先排序后合并,排序可以使用归并。当然,最好的方式是使用stl给的内置排序方式,可以参考:https://leetcode.com/problems/merge-intervals/discuss/21242/C%2B%2B-10-line-solution.-easing-understanding
    其中,使用匿名函数来进行排序的方式如下:
    sort(ins.begin(), ins.end(), [](Interval a, Interval b){return a.start < b.start;});
  5. 58 Length of Last Word
  6. 59 Spiral Matrix II
    题意:还是spiral的一类问题
  7. 60 Permutation Sequence
    题意:permutation的题目
    思路:大概可以使用stl的next_permutation函数来求解吧。不知道合不合适。。。肯定是可解的,但是排名很低。下面这种解法用时最少。。。
class Solution {
public:
    //O(N^2), O(N)
    string getPermutation(int n, int k) {
        // initialize a dictionary that stores 1, 2, ..., n. This string will store the permutation.
        string dict(n, 0);
        iota(dict.begin(), dict.end(), '1');

        // build up a look-up table, which stores (n-1)!, (n-2)!, ..., 1!, 0!
        vector fract(n, 1);
        for (int idx = n - 3; idx >= 0; --idx) {
            fract[idx] = fract[idx + 1] * (n - 1 - idx);
        }

        // let k be zero base
        --k;

        // the main part.
        string ret(n, 0);
        for (int idx = 0; idx < n; ++idx) {
            int select = k / fract[idx];
            k %= fract[idx];
            ret[idx] = dict[select];
            dict.erase(next(dict.begin(), select)); // note that it is an O(n) operation
        }
        return ret;
    }
};

你可能感兴趣的:(leetcode轮回计划20180923)