###### tags: `LeetCode` `Medium` # LeetCode #55 [Jump Game](https://leetcode.com/problems/jump-game) ### (Medium) 給定一個非負整數數組 nums ,你最初位於數組的第一個下標 。 數組中的每個元素代表你在該位置可以跳躍的最大長度。 判斷你是否能夠到達最後一個下標。 --- 設一變數reach代表從起點開始能走到的最遠距離, 並令變數i代表檢查點, 使用for迴圈遍歷nums, 若i=n(成功走到終點)或i>reach(斷檔了, 途中有0的出現導致無法通過而出現目前的檢查點位置超過能到達的最遠距離)時終止。 最後檢查i是否等於n(終點位置)。 --- ``` class Solution { public: bool canJump(vector<int>& nums) { int i = 0, n= nums.size(); for (int reach = 0; i < n && i <= reach; ++i){ //i>reach ==> 斷檔了 reach = max(i + nums[i], reach); } return i == n; } }; ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up