|
35 | 35 | <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li>
|
36 | 36 | </ul>
|
37 | 37 |
|
38 | | - |
39 | 38 | ## Solutions
|
40 | 39 |
|
41 | 40 | <!-- tabs:start -->
|
42 | 41 |
|
43 | 42 | ### **Python3**
|
44 | 43 |
|
45 | 44 | ```python
|
46 | | - |
| 45 | +class Solution: |
| 46 | + def canJump(self, nums: List[int]) -> bool: |
| 47 | + mx = 0 |
| 48 | + for i, num in enumerate(nums): |
| 49 | + if i > mx: |
| 50 | + return False |
| 51 | + mx = max(mx, i + num) |
| 52 | + return True |
47 | 53 | ```
|
48 | 54 |
|
49 | 55 | ### **Java**
|
50 | 56 |
|
51 | 57 | ```java
|
| 58 | +class Solution { |
| 59 | + public boolean canJump(int[] nums) { |
| 60 | + int mx = 0; |
| 61 | + for (int i = 0; i < nums.length; ++i) { |
| 62 | + if (i > mx) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + mx = Math.max(mx, i + nums[i]); |
| 66 | + } |
| 67 | + return true; |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +### **C++** |
| 73 | + |
| 74 | +```cpp |
| 75 | +class Solution { |
| 76 | +public: |
| 77 | + bool canJump(vector<int>& nums) { |
| 78 | + int mx = 0; |
| 79 | + for (int i = 0; i < nums.size(); ++i) { |
| 80 | + if (i > mx) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + mx = max(mx, i + nums[i]); |
| 84 | + } |
| 85 | + return true; |
| 86 | + } |
| 87 | +}; |
| 88 | +``` |
| 89 | + |
| 90 | +### **Go** |
| 91 | + |
| 92 | +```go |
| 93 | +func canJump(nums []int) bool { |
| 94 | + mx := 0 |
| 95 | + for i, num := range nums { |
| 96 | + if i > mx { |
| 97 | + return false |
| 98 | + } |
| 99 | + mx = max(mx, i+num) |
| 100 | + } |
| 101 | + return true |
| 102 | +} |
| 103 | + |
| 104 | +func max(a, b int) int { |
| 105 | + if a > b { |
| 106 | + return a |
| 107 | + } |
| 108 | + return b |
| 109 | +} |
| 110 | +``` |
52 | 111 |
|
| 112 | +### **C#** |
| 113 | + |
| 114 | +```cs |
| 115 | +public class Solution { |
| 116 | + public bool CanJump(int[] nums) { |
| 117 | + int mx = 0; |
| 118 | + for (int i = 0; i < nums.Length; ++i) |
| 119 | + { |
| 120 | + if (i > mx) |
| 121 | + { |
| 122 | + return false; |
| 123 | + } |
| 124 | + mx = Math.Max(mx, i + nums[i]); |
| 125 | + } |
| 126 | + return true; |
| 127 | + } |
| 128 | +} |
53 | 129 | ```
|
54 | 130 |
|
55 | 131 | ### **...**
|
|
0 commit comments