Combination Sum | & || & ||| & IV
Combination Sum |
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is: [7] [2, 2, 3]
Notice
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
given candidate set 2,3,6,7 and target 7,
A solution set is: [7] [2, 2, 3]
分析:递归
public class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
}
public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<>(list));
}
helper(index, total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
helper(index + , total, candidates, target, list, listsAll);
}
}
Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C may only be used once in the combination.
Notice
- All numbers (including target) will be positive integers.
- Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
- The solution set must not contain duplicate combinations.
Given candidate set [10,1,6,7,2,1,5] and target 8,
A solution set is:
[
[1,7],
[1,2,5],
[2,6],
[1,1,6]
]
public class Solution {
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
List<List<Integer>> listsAll = new ArrayList<List<Integer>>();
Arrays.sort(candidates);
helper(, , candidates, target, new ArrayList<>(), listsAll);
return listsAll;
}
public static void helper(int index, int total, int[] candidates, int target, List<Integer> list, List<List<Integer>> listsAll) {
if (index >= candidates.length || total >= target) return;
list.add(candidates[index]);
total += candidates[index];
if (total == target) {
listsAll.add(new ArrayList<Integer>(list));
}
helper(index + , total, candidates, target, list, listsAll);
total = total - candidates[index];
list.remove(list.size() - );
while (index + < candidates.length && candidates[index] == candidates[index + ]) {
index++;
}
helper(index + , total, candidates, target, list, listsAll);
}
}
Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.
Example:
nums = [1, 2, 3]
target = 4 The possible combination ways are:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1) Note that different sequences are counted as different combinations. Therefore the output is 7.
分析: 这题和change coin非常相似。
public class Solution {
public int combinationSum4(int[] nums, int target) {
if (nums == null || nums.length == ) return ;
int[] dp = new int[target + ];
dp[] = ;
for (int i = ; i <= target; i++) {
for (int num : nums) {
if (i - num >= ) {
dp[i] += dp[i - num];
}
}
}
return dp[target];
}
}
参考请注明出处:cnblogs.com/beiyeqingteng/
Combination Sum | & || & ||| & IV的更多相关文章
- LC 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
- [LeetCode] 377. Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] 377. Combination Sum IV 组合之和 IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- 377. Combination Sum IV
问题 Given an integer array with all positive numbers and no duplicates, find the number of possible c ...
- Leetcode 377. Combination Sum IV
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- leetcode日记 Combination sum IV
题目: Given an integer array with all positive numbers and no duplicates, find the number of possible ...
- Leetcode: Combination Sum IV && Summary: The Key to Solve DP
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- Mathematical operation
(1)Using let let result=2+1 let result=2-1 let result=2*1 let result=2/1(2) Using bracket echo $(($p ...
- TCP/IP详解 学习七
静态选路的前提: 1) 网络比较小 2) 网络之间单点连接 3) 网络之间没有多余的路由 动态选路协议,用于路由器之间的通信,有以下几种: 1) ...
- MyEclipse------在特定目录创建文件和书写内容
readwrite.jsp <%@ page language="java" import="java.util.*" pageEncoding=&quo ...
- wireshark怎么抓包、wireshark抓包详细图文教程
wireshark怎么抓包.wireshark抓包详细图文教程 作者:佚名 来源:本站整理 发布时间:2013-05-02 19:56:27 本日:53 本周:675 本月:926 总数:3749 ...
- 一个C++类的注释:
#ifndef __RUNTIMEPARA__HPP#define __RUNTIMEPARA__HPP #include <string> //后面会有介绍 #include <m ...
- ASP.Net 5 上传文件通过虚拟路径存储
先贴上代码 [HttpPost] public IActionResult ImportTeaching(IFormFile file) { string root = @"Temp/tea ...
- JS代码的加载
HTML页面中JS的加载原理:在加载HTML页面的时候,当浏览器遇到内嵌的JS代码时会停止处理页面,先执行JS代码,然后再继续解析和渲染页面.同样的情况也发生在外链的JS文件中,浏览器必须先花时间下载 ...
- XSS 探索
1. 什么是XSS攻击? 正常的页面被渗出了攻击者的js脚本,这些脚本可以非法地获取用户信息,然后将信息发送到attacked的服务端. XSS是需要充分利用输出环境来构造攻击脚本的 2. 危害 非法 ...
- Cocos2d-x 3.0修改Android平台帧率fps - 解决游戏运行手机发热发烫问题
使用Cocos2d-x 3.0开发游戏之后,发现游戏在android手机上发热非常严重,在魅族2上,几乎担心手机会爆炸了~~~采取的一个措施就是降低帧率,因为游戏对于帧率要求不是非常高. 做过coco ...
- PHP数组的交集array_intersect(),array_intersect_assoc(),array_inter_key()函数详解
求两个数组的交集问题可以使用 array_intersect(),array_inersect_assoc,array_intersect_key来实现,其中 array_intersect()函数是 ...