首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁55.Jump Game”︁的源代码
←
55.Jump Game
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
=Description= {{LeetCode |id=jump-game |no=55 |difficulty=Medium |category=Dynamic Programming |collection=Top 150 |title=Triangle |summary=You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise.}} Example 1: <syntaxhighlight lang="bash"> Input: nums = [2,3,1,1,4] Output: true </syntaxhighlight> Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: <syntaxhighlight lang="bash"> Input: nums = [3,2,1,0,4] Output: false </syntaxhighlight> Explanation: You will always arrive at index 3 no matter what. Its maximum jump length is 0, which makes it impossible to reach the last index. =Solution= ==1D DP== {{Submission|runtime=418ms|memory=43.4MB|rp=7.2|mp=97.69}} <syntaxhighlight lang="java"> class Solution { public boolean canJump(int[] nums) { /* [2,3,1,1,4] 1 1 2 2 2 */ int []dp = new int[nums.length]; dp[0] = 1; for(int i = 0; i < nums.length; i++) { for(int j = 1; j <= nums[i]; j++){ if(i+j >= dp.length) break; dp[i+j]++; } } for(int i = 0; i < dp.length; i++) { if(dp[i] == 0) return false; } return true; } } </syntaxhighlight> ==1D DP 2== {{Submission|runtime=433ms|memory=44.03MB|rp=6.85|mp=70.05}} <syntaxhighlight lang="java"> class Solution { public boolean canJump(int[] nums) { boolean []dp = new boolean[nums.length]; dp[0] = true; for(int i = 0; i <nums.length; i++) { if(!dp[i]) return false; for(int j = 1; j <= nums[i]; j++) { int next = i + j; if(next >= dp.length) break; dp[next] = true; } } return dp[nums.length-1]; } } </syntaxhighlight>
此页面嵌入的页面:
模板:LeetCode
(
查看源代码
)
模板:Submission
(
查看源代码
)
返回
55.Jump Game
。