classSolution { public: intjump(std::vector<int>& nums){ int n = nums.size(); if (n == 1) { return0; } int steps = 0; int max_reach = 0; int end = 0; for (int i = 0; i < n - 1; ++i) { max_reach = std::max(max_reach, i + nums[i]); if (i == end) { end = max_reach; ++steps; } } return steps; } };
结果
执行用时 : 11 ms, 击败 74.69% 使用 C++ 的用户
内存消耗 : 18.50 MB, 击败 14.62% 使用 C++ 的用户
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintjump(int[] nums) { intn= nums.length; if (n == 1) { return0; } intsteps=0; intmaxReach=0; intend=0; for (inti=0; i < n - 1; ++i) { maxReach = Math.max(maxReach, i + nums[i]); if (i == end) { end = maxReach; ++steps; } } return steps; } }
结果
执行用时 : 1 ms, 击败 99.09% 使用 Java 的用户
内存消耗 : 44.09 MB, 击败 17.59% 使用 Java 的用户
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution(object): defjump(self, nums): """ :type nums: List[int] :rtype: int """ n = len(nums) if n == 1: return0 steps = 0 max_reach = 0 end = 0 for i inrange(n - 1): max_reach = max(max_reach, i + nums[i]) if i == end: end = max_reach steps += 1 return steps
结果
执行用时 : 20 ms, 击败 97.05% 使用 Python 的用户
内存消耗 : 12.15 MB, 击败 94.93% 使用 Python 的用户
Python3
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution: defjump(self, nums: List[int]) -> int: n = len(nums) if n == 1: return0 steps = 0 max_reach = 0 end = 0 for i inrange(n - 1): max_reach = max(max_reach, i + nums[i]) if i == end: end = max_reach steps += 1 return steps
结果
执行用时 : 49 ms, 击败 73.77% 使用 Python3 的用户
内存消耗 : 17.19 MB, 击败 43.74% 使用 Python3 的用户
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
intjump(int* nums, int numsSize) { if (numsSize == 1) { return0; } int steps = 0; int max_reach = 0; int end = 0; for (int i = 0; i < numsSize - 1; ++i) { max_reach = max_reach > i + nums[i] ? max_reach : i + nums[i]; if (i == end) { end = max_reach; ++steps; } } return steps; }
结果
执行用时 : 14 ms, 击败 43.78% 使用 C 的用户
内存消耗 : 6.32 MB, 击败 95.87% 使用 C 的用户
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintJump(int[] nums) { int n = nums.Length; if (n == 1) { return0; } int steps = 0; int maxReach = 0; int end = 0; for (int i = 0; i < n - 1; ++i) { maxReach = Math.Max(maxReach, i + nums[i]); if (i == end) { end = maxReach; ++steps; } } return steps; } }
/** * @param {number[]} nums * @return {number} */ var jump = function(nums) { const n = nums.length; if (n === 1) { return0; } let steps = 0; let maxReach = 0; let end = 0; for (let i = 0; i < n - 1; ++i) { maxReach = Math.max(maxReach, i + nums[i]); if (i === end) { end = maxReach; ++steps; } } return steps; };
结果
执行用时 : 63 ms, 击败 72.09% 使用 JavaScript 的用户
内存消耗 : 51.27 MB, 击败 5.23% 使用 JavaScript 的用户
TypeScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
functionjump(nums: number[]): number { constn: number = nums.length; if (n === 1) { return0; } letsteps: number = 0; letmaxReach: number = 0; letend: number = 0; for (let i = 0; i < n - 1; ++i) { maxReach = Math.max(maxReach, i + nums[i]); if (i === end) { end = maxReach; ++steps; } } return steps; }
classSolution { funcjump(_nums: [Int]) -> Int { let n = nums.count if n ==1 { return0 } var steps =0 var maxReach =0 var end =0 for i in0..<n-1 { maxReach =max(maxReach, i + nums[i]) if i == end { end = maxReach steps +=1 } } return steps } }
结果
执行用时 : 27 ms, 击败 100.00% 使用 Swift 的用户
内存消耗 : 15.43 MB, 击败 29.41% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { funjump(nums: IntArray): Int { val n = nums.size if (n == 1) { return0 } var steps = 0 var maxReach = 0 var end = 0 for (i in0 until n - 1) { maxReach = maxOf(maxReach, i + nums[i]) if (i == end) { end = maxReach steps += 1 } } return steps } }
结果
执行用时 : 192 ms, 击败 97.10% 使用 Kotlin 的用户
内存消耗 : 38.05 MB, 击败 52.17% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution{ int jump(List<int> nums) { int n = nums.length; if (n == 1) { return0; } int steps = 0; int maxReach = 0; int end = 0; for (int i = 0; i < n - 1; ++i) { maxReach = nums[i] + i > maxReach ? nums[i] + i : maxReach; if (i == end) { end = maxReach; ++steps; } } return steps; } }
funcjump(nums []int)int { n := len(nums) if n == 1 { return0 } steps := 0 maxReach := 0 end := 0 for i := 0; i < n-1; i++ { maxReach = max(maxReach, i+nums[i]) if i == end { end = maxReach steps++ } } return steps } funcmax(a, b int)int { if a > b { return a } return b }
结果
执行用时 : 8 ms, 击败 92.06% 使用 Go 的用户
内存消耗 : 5.89 MB, 击败 98.04% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# @param {Integer[]} nums # @return {Integer} defjump(nums) n = nums.length return0if n == 1 steps = 0 max_reach = 0 end_pos = 0 (0..n-2).each do |i| max_reach = [max_reach, i + nums[i]].max if i == end_pos end_pos = max_reach steps += 1 end end steps end
结果
执行用时 : 57 ms, 击败 85.71% 使用 Ruby 的用户
内存消耗 : 207.21 MB, 击败 71.43% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
objectSolution{ defjump(nums: Array[Int]): Int = { val n = nums.length if (n == 1) { return0 } var steps = 0 var maxReach = 0 var endPos = 0 for (i <- 0 until n - 1) { maxReach = math.max(maxReach, i + nums(i)) if (i == endPos) { endPos = maxReach steps += 1 } } steps } }
结果
执行用时 : 536 ms, 击败 100.00% 使用 Scala 的用户
内存消耗 : 55.57 MB, 击败 30.00% 使用 Scala 的用户
Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
implSolution { pubfnjump(nums: Vec<i32>) ->i32 { letn = nums.len(); if n == 1 { return0; } letmut steps = 0; letmut max_reach = 0usize; letmut end = 0usize; foriin0..n - 1 { max_reach = max_reach.max(i + nums[i] asusize); if i == end { end = max_reach; steps += 1; } } steps } }