150強訪談簡單

跳躍遊戲

“跳躍遊戲”問題的詳細指南和 Python 實現。

問題陳述

簡單

給定一個整數數組 nums。您最初位于数组的第一个索引处,数组中的每个元素代表您在该位置的最大跳跃长度。

如果可以到達最後一個索引,則傳回 True,否則傳回 False。

寫一個函數 canJump(nums: List[int]) -> bool

約束條件
  • 1 <= len(nums) <= 10^4
  • 0 <= nums[i] <= 10^5

範例

Example 1
Input
nums = [2,3,1,1,4]
Output
True
Explanation

Jump 1 step from index 0 to 1, then 3 steps to the last index.

Example 2
Input
nums = [3,2,1,0,4]
Output
False
Explanation

You will always arrive at index 3. Its maximum jump length is 0, which makes it impossible to reach the last index.

Need a Hint?
考慮使用特定於貪婪的資料結構,例如集合或堆疊。
Edge Cases to Watch
  • 空輸入結構
  • 單元素輸入
  • 大數值範圍

準備好解決了嗎?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

在編輯器中開啟
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

推薦的 Python 資源

透過相關的互動式教學、備忘單和程式碼比較來擴展您的知識。