135. 数字组合

中文
English

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

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

样例

样例 1:

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

样例 2:

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

注意事项

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

153. 数字组合 II

中文
English

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

样例

样例 1:

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

样例 2:

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

注意事项

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

90. k数和 II

中文
English

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

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

样例

样例 1:

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

样例 2:

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

  3. 这个题目更简单,直接dfs模板即可做。
  1. class Solution:
  2. """
  3. @param: A: an integer array
  4. @param: k: a postive integer <= length(A)
  5. @param: targer: an integer
  6. @return: A list of lists of integer
  7. """
  8. def kSumII(self, A, k, target):
  9. # write your code here
  10. path = []
  11. result = []
  12. self.dfs(A, k, target, path, result, start_index=0)
  13. return result
  14.  
  15. def dfs(self, arr, k, target, path, result, start_index):
  16. if target == 0 and k == 0:
  17. result.append(list(path))
  18. return
  19.  
  20. if target < 0 or k <= 0:
  21. return
  22.  
  23. for i in range(start_index, len(arr)):
  24. path.append(arr[i])
  25. self.dfs(arr, k-1, target-arr[i], path, result, i+1)
  26. path.pop()
  1.  

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 Boot 排除自动配置的 4 种方法,关键时刻很有用!

    Spring Boot 提供的自动配置非常强大,某些情况下,自动配置的功能可能不符合我们的需求,需要我们自定义配置,这个时候就需要排除/禁用 Spring Boot 某些类的自动化配置了. 比如:数据 ...

  2. Shell脚本之一 Shell脚本简介

    一.什么是shell? 我们平时所说的 Shell 可以理解为 Linux 系统提供给用户的使用界面.Shell 为用户提供了输入命令和参数并可得到命令执行结果的环境.当一个用户登录 Linux 之后 ...

  3. php Access-Control-Allow-Origin 解决跨域问题

    第1种 在代码里面加 header信息(推荐) header("Access-Control-Allow-Origin: *"); //如果需要设置允许所有域名发起的跨域请求,可以 ...

  4. git安装和使用配置

    1.简介 Git是一个开源的分布式版本控制系统,能用于快速高效地处理任何或小或大的项目,它是Linus Torvalds为了帮助管理Linux内核开发而开发的一个源码开放的版本控制软件. 2.Linu ...

  5. [转帖]Java Netty简介

    Java Netty简介 https://www.cnblogs.com/ghj1976/p/3779820.html Posted on 2014-06-10 13:41 蝈蝈俊 阅读(2992) ...

  6. Ext.net自动保存读取GrdPanel列显示状态

    //layout保存 function SaveLayOut() { let colVisibleArray = []; for (var i = 0; i < mcp_gridlist.col ...

  7. Effective.Java第34-44条(枚举)

    34.  使用枚举类型替代整型常量 常量的语义表达不清晰,只能靠前面的名称来区分.枚举具有可读性.更安全.更强大等优势.而且枚举类型对象之间的值比较可以使用==来比较值是否相等的,不是必须使用equa ...

  8. BJFU-225-基于链表的两个递增有序序列的合并

    #include<stdio.h> #include<stdlib.h> typedef struct Lnode{ int num; struct Lnode * next; ...

  9. golang面对对象

  10. Java学习:构造方法

    构造方法: 构造方法是专门用来创建对象的方法,当我们通过关键字new来创建对象时,其实就是再调用构造函数. 格式: public 类名称(参数类型 参数名称){ 方法体 } 注意事项: 构造方法的名称 ...