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. Spring boot2X集成zuul与consul实现负载均衡和反向代理

    zuul 是netflix开源的一个API Gateway 服务器 所有从设备或网站来的请求都会经过Zuul到达后端的Netflix应用程序. 作为一个边界性质的应用程序,Zuul提供了动态路由.监控 ...

  2. ASP.NET Core WebApi构建API接口服务实战演练

    一.ASP.NET Core WebApi课程介绍 人生苦短,我用.NET Core!提到Api接口,一般会想到以前用到的WebService和WCF服务,这三个技术都是用来创建服务接口,只不过Web ...

  3. thinkphp伪静态怎么实现

    thinkphp如何实现伪静态? 去掉 URL 中的 index.php ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好.但 ThinkPHP 提供了各种机制 ...

  4. ps 简单使用 ----- 将图片静态图片制作成动图

    1. 准备好你需要静态图片  放到文件夹中 2.打开ps 软件    将右上角切换为   动感 3.观察下方时间轴   点击加号   将图片导入 按住shift  选择图片 4.切换为帧格式 选择循环 ...

  5. 将Javabean转化JSONObject为对象

    JSONObject.parseObject(JSONObject.toJSON(obj).toString()):

  6. Npoi 的使用

    npoi这个office写入,我个人有点不方便,但是因为需要使用所以不得不去用了. 原因: 1. 没文档 2. 网上的案例版本不同 3. 对于复杂列不好做处理 跟网上其他工具的对比,好处就是不需要依赖 ...

  7. 【学习笔记】Docker基础

    基本概念 Docker是什么? Docker是一种基于Golang开发的虚拟化技术,开发人员和系统管理员使用容器开发,部署和运行应用程序的平台. 使用Linux容器部署应用程序称为容器化. 容器不是新 ...

  8. Java学习:等待唤醒机制

    等待唤醒机制 线程的状态 NEW   至今尚未启动的线程处于这种状态 RUNNABLE   正在Java虚拟机中执行的线程处于这种状态 BLOCKED 受阻塞并等待某个监视器锁的线程处于这种状态 WA ...

  9. SQL系列(六)—— 过滤(where)

    在日常的应用中的,大多数业务场景都只是需要特定的数据,所以能够过滤筛选数据显得尤为至关重要.从需求角度分析,需要特定的数据,即需要一定条件的数据,即从全量数据中根据特定条件过滤出需要的数据. 如果需要 ...

  10. Windows定时清理文件处理脚本

    一.运行CMD,输入forfile/?,即可获取forfile的使用方法 /P  路径 /M  文件类型 /D  时间   + | -     +:之后    - :之前   example:-2   ...