当前位置:网站首页>LeetCode练习——跳跃游戏、组合求和
LeetCode练习——跳跃游戏、组合求和
2022-06-24 06:47:00 【是妮妮呢】
跳跃游戏 II
题目
给你一个非负整数数组 nums ,你最初位于数组的第一个位置。
数组中的每个元素代表你在该位置可以跳跃的最大长度。
你的目标是使用最少的跳跃次数到达数组的最后一个位置。
假设你总是可以到达数组的最后一个位置。
示例
输入: nums = [2,3,1,1,4]
输出: 2
解释: 跳到最后一个位置的最小跳跃数是 2。
从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个位置。
输入: nums = [2,3,0,1,4]
输出: 2
代码
class Solution(object):
def jump(self, nums):
if len(nums) == 1: return 0
ans = 0 #表示最远到达的位置
curDistance = 0 # 参数初始化简化写法
nextDistance = 0
for i in range(len(nums)): #遍历nums中的每个位置
nextDistance = max(i + nums[i], nextDistance) # 下一个跳跃位置
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
return ans
组合求和 II
题目
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。
candidates 中的每个数字在每个组合中只能使用 一次 。
说明:
所有数字(包括target)都是正整数
解集不能包括重复的组合
示例
输入: candidates = [10,1,2,7,6,1,5], target = 8,
输出:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
输入: candidates = [2,5,2,1,2], target = 5,
输出:
[
[1,2,2],
[5]
]
代码
class Solution(object):
def combinationSum2(self, candidates, target): #组合求和
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
res = []
path = []
def backtrack(candidates,target,sum,startIndex):
if sum == target: res.append(path[:])
for i in range(startIndex,len(candidates)): #要对同一数层使用过的元素进行跳过
if sum + candidates[i] > target: return
if i > startIndex and candidates[i] == candidates[i-1]: continue #直接用startIndex来去重,要对同一树层使用过得元素进行跳过
sum += candidates[i]
path.append(candidates[i])
backtrack(candidates,target,sum,i+1) # i+1 每个数字在每个组合中只能使用一次
sum -= candidates[i] #回溯法
path.pop()
candidates = sorted(candidates) #进行排序
backtrack(candidates,target,0,0)
return res
边栏推荐
- 面试中的最常被问到的两种锁
- Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible
- Timer usage notes
- Exploration on Optimization of elastic expansion engineering
- What kind of experience is it when the Institute earns 20000 yuan a month!
- Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
- POM configuration provided and test
- 报错“Computation failed in `stat_summary_hex()`”
- Global and Chinese market of Earl Grey tea 2022-2028: Research Report on technology, participants, trends, market size and share
- uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效
猜你喜欢

调用Feign接口时指定ip

Oracle-高级SQL限定查询

火线,零线,地线,你知道这三根线的作用是什么吗?

保留一位小数和保留两位小数

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA

RDD的执行原理

uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效

Resolution error: LNK2019 unresolved external symbol

Exness: Powell insisted on his anti inflation commitment and pointed out that recession is possible

本地备份和还原 SQL Server 数据库
随机推荐
Shader 常用函数
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
Duilib display memory picture
Win10 build webservice
Timer usage notes
Anaconda 中使用 You Get
10. Tencent cloud IOT device side learning - firmware upgrade
Hilbert Huang Transform
Oracle-高级SQL限定查询
云开发谁是卧底小程序源码
《canvas》之第4章 线条操作
Thread considerations
Quickly set up PgSQL for serverless
Hongmeng development IV
Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
用Ngrok 配置属于自己的免费外网域名
Exploration on Optimization of elastic expansion engineering
鸿蒙os开发三
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
线程注意事项