【44】Wildcard Matching
【45】Jump Game II (2018年11月28日,算法群衍生题)
题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步。
题解:
【55】Jump Game (2018年11月27日,算法群)
给了一个数组nums,nums[i] = k 代表站在第 i 个位置的情况下, 我最多能往前走 k 个单位。问我能不能到达最后一个 index。
题解:虽然是贪心分类,我还是用dp解了。dp[i] 代表我能不能到达第 i 个位置。
1 class Solution { 2 public: 3 bool canJump(vector<int>& nums) { 4 const int n = nums.size(); 5 if (n == 0) {return false;} 6 vector<int> f(n, 0); //f[i] 代表第i个index是不是可达 7 f[0] = 1; 8 for (int i = 0; i < n; ++i) { 9 if (f[i]) { 10 const int k = nums[i]; 11 for (int j = 1; j <= k; ++j) { 12 if (i+j >= n) {break;} 13 f[i+j] = 1; 14 } 15 } 16 } 17 return f[n-1] == 1; 18 } 19 };