135. 数字组合

中文
English

给定一个候选数字的集合 candidates 和一个目标值 target. 找到 candidates 中所有的和为 target 的组合.

在同一个组合中, candidates 中的某个数字不限次数地出现.

样例

样例 1:

输入: candidates = [2, 3, 6, 7], target = 7
输出: [[7], [2, 2, 3]]

样例 2:

输入: candidates = [1], target = 3
输出: [[1, 1, 1]]

注意事项

  1. 所有数值 (包括 target ) 都是正整数.
  2. 返回的每一个组合内的数字必须是非降序的.
  3. 返回的所有组合之间可以是任意顺序.
  4. 解集不能包含重复的组合.
class Solution:
"""
@param candidates: A list of integers
@param target: An integer
@return: A list of lists of integers
"""
def combinationSum(self, candidates, target):
# write your code here
result = []
self.dfs(sorted(list(set(candidates))), target, result, path=[], start_index=0)
return result def dfs(self, nums, target, result, path, start_index):
if target == 0 and path:
result.append(list(path)) if target < 0:
return for i in range(start_index, len(nums)):
path.append(nums[i])
self.dfs(nums, target-nums[i], result, path, i)
path.pop()

153. 数字组合 II

中文
English

给定一个数组 num 和一个整数 target. 找到 num 中所有的数字之和为 target 的组合.

样例

样例 1:

输入: num = [7,1,2,5,1,6,10], target = 8
输出: [[1,1,6],[1,2,5],[1,7],[2,6]]

样例 2:

输入: num = [1,1,1], target = 2
输出: [[1,1]]
解释: 解集不能包含重复的组合

注意事项

  1. 在同一个组合中, num 中的每一个数字仅能被使用一次.
  2. 所有数值 (包括 target ) 都是正整数.
  3. 返回的每一个组合内的数字必须是非降序的.
  4. 返回的所有组合之间可以是任意顺序.
  5. 解集不能包含重复的组合.
class Solution:
"""
@param num: Given the candidate numbers
@param target: Given the target number
@return: All the combinations that sum to target
"""
def combinationSum2(self, nums, target):
# write your code here
result = []
self.dfs(sorted(nums), target, result, path=[], start_index=0)
return result def dfs(self, nums, target, result, path, start_index):
if target == 0 and path:
result.append(list(path)) if target < 0:
return for i in range(start_index, len(nums)):
if i > 0 and nums[i] == nums[i-1] and i > start_index:
continue
path.append(nums[i])
self.dfs(nums, target-nums[i], result, path, i+1)
path.pop()

90. k数和 II

中文
English

给定n个不同的正整数,整数k(1<= k <= n)以及一个目标数字。    

在这n个数里面找出K个数,使得这K个数的和等于目标数字,你需要找出所有满足要求的方案。

样例

样例 1:

输入: [1,2,3,4], k = 2, target = 5
输出: [[1,4],[2,3]]

样例 2:

输入: [1,3,4,6], k = 3, target = 8
输出: [[1,3,4]]

这个题目更简单,直接dfs模板即可做。
class Solution:
"""
@param: A: an integer array
@param: k: a postive integer <= length(A)
@param: targer: an integer
@return: A list of lists of integer
"""
def kSumII(self, A, k, target):
# write your code here
path = []
result = []
self.dfs(A, k, target, path, result, start_index=0)
return result def dfs(self, arr, k, target, path, result, start_index):
if target == 0 and k == 0:
result.append(list(path))
return if target < 0 or k <= 0:
return for i in range(start_index, len(arr)):
path.append(arr[i])
self.dfs(arr, k-1, target-arr[i], path, result, i+1)
path.pop()

dfs --path sum 问题 本质上就是组合问题(有去重)的更多相关文章

  1. leetcode:Minimum Path Sum(路线上元素和的最小值)【面试算法题】

    题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right w ...

  2. [LeetCode] Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  3. [LeetCode] 113. Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. 【LeetCode】113. Path Sum II

    Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...

  6. Path Sum II 总结DFS

    https://oj.leetcode.com/problems/path-sum-ii/ Given a binary tree and a sum, find all root-to-leaf p ...

  7. leetcode@ [124] Binary Tree Maximum Path Sum (DFS)

    https://leetcode.com/problems/binary-tree-maximum-path-sum/ Given a binary tree, find the maximum pa ...

  8. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. leetcode 784. Letter Case Permutation——所有BFS和DFS的题目本质上都可以抽象为tree,这样方便你写代码

    Given a string S, we can transform every letter individually to be lowercase or uppercase to create ...

随机推荐

  1. Java判断文件类型

    通常,在WEB系统中,上传文件时都需要做文件的类型校验,大致有如下几种方法: 1. 通过后缀名,如exe,jpg,bmp,rar,zip等等. 2. 通过读取文件,获取文件的Content-type来 ...

  2. 安装gerrit服务器

    一.环境准备 1.Java环境 gerrit依赖,用于安装gerrit环境. 下载:jdk-7u79-linux-x64.tar.gz http://www.oracle.com/technetwor ...

  3. MySQL 5.7更改Root密码和Host

    MySQL5.7对用户密码管理对更为严格. 为测试方便,需要将root对权限扩大为所有host.具体操作如下: grep 'temporary password' /var/log/mysqld.lo ...

  4. C# 获取url中的查询字符串参数

    /// <summary> /// 获取url中的查询字符串参数 /// </summary> public static NameValueCollection Extrac ...

  5. C语言函数调用

    1.backtrace一些内存检测工具如Valgrind,调试工具如GDB,可以查看程序运行时函数调用的堆栈信息,有时候在分析程序时要获得堆栈信息,借助于backtrace是很有帮助的,其原型如下: ...

  6. springmvc接收参数为日期类型

    用单个Date类型接收日期类型时,会出现报错,加上initBinder的方法 意思是将所有传入的参数都通过此方法,如果过是日期通过日期格式化器进行格式化 如果是接收类型为对象内的属性为Date类型时 ...

  7. 「NOI2018」冒泡排序

    「NOI2018」冒泡排序 考虑冒泡排序中一个位置上的数向左移动的步数 \(Lstep\) 为左边比它大的数的个数,向右移动的步数 \(Rstep\) 为右边比它大的数的个数,如果 \(Lstep,R ...

  8. Direct Buffer介绍

    Direct Buffer 前言 上篇文章Buffer末尾中谈到堆内Buffer(Heap Buffer)和直接Buffer(Direct Buffer)的概念,但是却一笔带过,并未涉及其细节,这篇文 ...

  9. mingw 编译 glfw3 的 helloworld

    glfw3 为基础开发 GUI 似乎是一个不错选项,有很多人尝试这么做了.今天也小试一把. 工具: mingw(不是 mingw-w64),头文件 GLFW/ ,库文件 glfw3.dll 需要注意, ...

  10. 能写数据后台,需要掌握哪些进阶的sql语句?

    国庆假期花了一些时间,首次尝试并玩转 grafana,这几天继续不断优化和完善,如今看着自己的成果,相当满意.--逐步接近我想要的理想后台啦. 需求是不停歇的.今天我又给自己发掘了一些新需求,比如变量 ...