Re: [闲聊] 每日LeetCode

楼主: umi0912umi (UMI)   2023-02-08 18:21:08
: 45. Jump Game II
: 给你一个阵列nums,nums[i]表示从这个点可以往后移动几格,求出从第0格开始,最少
: 跳几格可以跳到阵列尾端(题目保证一定可以到阵列尾端)。
:  
: Example:
:  
: Input: nums = [2,3,1,1,4]
: Output: 2
: Explanation: The minimum number of jumps to reach the last index is 2. Jump 1
: step from index 0 to 1, then 3 steps to the last index.
:  
: Input: nums = [2,3,0,1,4]
: Output: 2
class Solution:
def jump(self, nums: List[int]) -> int:
count, now_pos = 0, 0
while now_pos < len(nums)-1 :
jump_step = nums[now_pos]
max_length = 0
if now_pos + jump_step >= len(nums)-1 :
return count+1
for i in range(jump_step) :
if nums[now_pos+i+1] == 0 :
continue
else :
max_length_t = nums[now_pos+i+1] + i
if max_length_t > max_length :
max_length = max_length_t
max_i = i+1
now_pos = now_pos + max_i
count += 1
return count
看惹一下别人写的code
跟我思路一样可是就是干净好多
还有原来可以max()里面在包for来比
我好烂
QQ
作者: SecondRun (雨夜琴声)   2023-02-08 18:22:00
大师
作者: heynui (天音かなた的兔)   2023-02-08 18:23:00
大师
作者: pandix (面包屌)   2023-02-08 18:25:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com