diff --git a/problems/55.jump-game.md b/problems/55.jump-game.md index eb666574f..863733321 100644 --- a/problems/55.jump-game.md +++ b/problems/55.jump-game.md @@ -46,8 +46,8 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum ## 代码 -- 语言支持: Javascript,Python3 - +- 语言支持: Javascript,C++,Java,Python3 +Javascript Code: ```js /** * @param {number[]} nums @@ -65,6 +65,55 @@ var canJump = function (nums) { }; ``` +C++ Code: + +```c++ +class Solution { +public: + bool canJump(vector& nums) { + int n=nums.size(); + int k=0; + for(int i=0;ik){ + return false; + } + // 能跳到最后一个位置 + if(k>=n-1){ + return true; + } + // 从当前位置能跳的最远的位置 + k = max(k, i+nums[i]); + } + return k >= n-1; + } +}; +``` + +Java Code: + +```java +class Solution { + public boolean canJump(int[] nums) { + int n=nums.length; + int k=0; + for(int i=0;ik){ + return false; + } + // 能跳到最后一个位置 + if(k>=n-1){ + return true; + } + // 从当前位置能跳的最远的位置 + k = Math.max(k, i+nums[i]); + } + return k >= n-1; + } +} +``` + Python3 Code: ```Python