当前位置:网站首页>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
边栏推荐
- Smart pointer remarks
- Jenkins 太老了 试试它?云原生 CI/CD Tekton
- 『C语言』系统日期&时间
- Ke Yi fallible point
- IndexError: Target 7 is out of bounds.
- New features of PHP: bytecode cache and built-in server
- js实现查看一个数组对象中是否包含另一个数组对象中的值
- 自动化测试的生命周期是什么?
- Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
- 爬虫基础B1——Scrapy(B站学习笔记)
猜你喜欢

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

关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题

Hilbert Huang Transform

慕思股份在深交所上市:毛利率持续下滑,2022年一季度营销失利

Basics of reptile B1 - scrapy (learning notes of station B)

Baidu map, coordinate inversion, picking coordinate position

Cloud development who is the source code of undercover applet

Hongmeng OS development III

本地备份和还原 SQL Server 数据库

exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
随机推荐
Oracle-高级SQL限定查询
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
保留一位小数和保留两位小数
语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)
Mysql database recovery case sharing
Climbing 10000 NASA pictures about Mars exploration, I found a secret
2022年PMP项目管理考试敏捷知识点(1)
『C语言』系统日期&时间
Domain environment importing Tencent cloud considerations
《canvas》之第3章 曲线图形
C code writing specification
RDD的执行原理
Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
Continue to have a fever. Try the asynchronous operation of dart language. The efficiency is increased by 500%
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
交友相亲类软件是如何割你韭菜的
Los Angeles p1051 who won the most Scholarships
语料库数据处理个案实例(句子检索相关个案)
调用Feign接口时指定ip
免费ICP域名备案查接口