当前位置:网站首页>Leetcode exercise - jumping game, combination summation
Leetcode exercise - jumping game, combination summation
2022-06-24 07:56:00 【It's nini】
Jumping game II
subject
Here's an array of nonnegative integers nums , You are first in the array .
Each element in the array represents the maximum length you can jump at that location .
Your goal is to reach the last position of the array with the least number of jumps .
Suppose you can always reach the last position of the array .
Example
Input : nums = [2,3,1,1,4]
Output : 2
explain : The minimum number of jumps to the last position is 2.
From the subscript for 0 Jump to subscript 1 The location of , jump 1 Step , Then jump 3 Step to the last position of the array .
Input : nums = [2,3,0,1,4]
Output : 2
Code
class Solution(object):
def jump(self, nums):
if len(nums) == 1: return 0
ans = 0 # Indicates the farthest position reached
curDistance = 0 # Parameter initialization simplified writing
nextDistance = 0
for i in range(len(nums)): # Traverse nums Each location in
nextDistance = max(i + nums[i], nextDistance) # Next jump position
if i == curDistance:
if curDistance != len(nums) - 1:
ans += 1
curDistance = nextDistance
if nextDistance >= len(nums) - 1: break
return ans
Combinatorial summation II
subject
Given an array without repeating elements candidates And a target number target , find candidates All can make numbers and target The combination of .
candidates Each number in can only be used in each combination once .
explain :
All figures ( Include target) Positive integers
The solution set cannot include duplicate combinations
Example
Input : candidates = [10,1,2,7,6,1,5], target = 8,
Output :
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]
Input : candidates = [2,5,2,1,2], target = 5,
Output :
[
[1,2,2],
[5]
]
Code
class Solution(object):
def combinationSum2(self, candidates, target): # Combinatorial summation
"""
: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)): # To skip elements used in the same layer
if sum + candidates[i] > target: return
if i > startIndex and candidates[i] == candidates[i-1]: continue # Direct use startIndex To and fro , To skip elements used in the same tree layer
sum += candidates[i]
path.append(candidates[i])
backtrack(candidates,target,sum,i+1) # i+1 Each number can only be used once in each combination
sum -= candidates[i] # backtracking
path.pop()
candidates = sorted(candidates) # Sort
backtrack(candidates,target,0,0)
return res
边栏推荐
- 《canvas》之第1章 canvas概述
- Los Angeles p1051 who won the most Scholarships
- Gossip: what happened to 3aC?
- 随机数备注
- Echart 心得 (一): 有关Y轴yAxis属性
- 基于Distiller的模型压缩工具简介
- These dependencies were not found: * core JS / modules / es6 array. Fill in XXX
- New features of PHP: bytecode cache and built-in server
- 自动化测试的生命周期是什么?
- 【Django中运行scrapy框架,并将数据存入数据库】
猜你喜欢

基于Distiller的模型压缩工具简介

Gossip: what happened to 3aC?

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

第 3 篇:绘制三角形

【NILM】非入侵式负荷分解模块nilmtk安装教程

AWTK 最新动态:Grid 控件新用法

Part 2: drawing a window

What kind of experience is it when the Institute earns 20000 yuan a month!

开放合作,共赢未来 | 福昕鲲鹏加入金兰组织

保留一位小数和保留两位小数
随机推荐
第 1 篇:搭建OpenGL环境
Leetcode 174 Dungeon games (June 23, 2022)
运行npm run eject报错解决方法
Gossip: what happened to 3aC?
opencvsharp二值图像反色
L1-019 谁先倒 (15 分)
线程注意事项
Bit operation
【NILM】非入侵式负荷分解模块nilmtk安装教程
Terminal network in VPN client connection settings of router
3-列表简介
面试中的最常被问到的两种锁
Introduction of model compression tool based on distiller
Moonwell Artemis is now online moonbeam network
开放合作,共赢未来 | 福昕鲲鹏加入金兰组织
Echart 心得 (一): 有关Y轴yAxis属性
Solution to the error of running NPM run eject
The two most frequently asked locks in the interview
线程的阻塞问题
《canvas》之第1章 canvas概述