055. Jump Game

方法一:递归,超时

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4         if (nums.size() < 2) return true;
 5         else {
 6             for (int i = 1; i <= *nums.begin(); ++i) {
 7                 vector<int> vec(nums.begin() + i, nums.end());
 8                 if (canJump(vec)) return true;
 9             }
10             return false;
11         }
12     }
13 };

方法二:动态规划

 1 class Solution {
 2 public:
 3     bool canJump(vector<int>& nums) {
 4         if (nums.size() < 2) return true;
 5         else {
 6             vector<int> reach(nums.size(), 0);
 7             reach[0] = 1;
 8             for (size_t i = 1; i < nums.size(); ++i) {
 9                 bool flag = false;
10                 for (int j = i - 1; j >= 0; --j) {
11                     if (reach[j] && nums[j] >= i - j) {
12                         flag = true;
13                         reach[i] = 1;
14                         break;
15                     }
16                 }
17                 if (!flag) return false;
18             }
19             return true;
20         }
21     }
22 };
时间: 2024-10-13 10:07:23

055. Jump Game的相关文章

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode]55.Jump Game

题目 Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:

Frog Jump

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water. Given a list of stones' positions (in units) in sorted ascending ord

LeetCode45 Jump Game II

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j

55. 45. Jump Game II *HARD*

1. Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A

LeetCode OJ:Jump Game(跳跃游戏)

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For example:A = 

LeetCode——Jump Game

Description: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. For

leetcode - Jump Game II

Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of jumps

【Jump Game II 】cpp

题目: Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of j