讲真,这道标榜为hard的题,我一下子捣鼓出来了还真有点儿受宠若惊。怎么说呢,经验就是,下键盘前一定要分析找到一条十分清晰的思路,良好的思路是成功的一半,加油~~~
My code:
class Solution(object):
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
max_len=0
flag=False
if len(nums)==0 or len(nums)==1:
return 0
for i in range(len(nums)):
if max_len<i: return 0
max_len=max(max_len,i+nums[i])
if max_len>=len(nums)-1:
flag=True
break
if flag==False:return 0
i=0
max_len=0
count=0
while(True):
temp=max_len
for k in range(i,max_len+1):
if nums[k]+k>temp:
temp=nums[k]+k
i=k
if temp>max_len:
count+=1
max_len=temp
if max_len>=len(nums)-1:
return count