- 浏览: 173510 次
- 性别:
- 来自: 济南
文章分类
最新评论
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.
Note:
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.
For example, given candidate set 2,3,6,7 and target 7,
A solution set is:
[7]
[2, 2, 3]
有关组合求和的问题,我们采用回溯法,从一条路走下去,如果找到结果就记录下来,如果走到尽头没找到结果就开始回溯,因为题目要求每个数字可以使用多次,因此我们往下寻找答案的起始点都是从当前元素开始。其次,题目要求结果集为非降序列,所以我们要先将数组排序在做处理。代码如下:
The same repeated number may be chosen from C unlimited number of times.
Note:
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.
For example, 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) { LinkedList<Integer> list = new LinkedList<Integer>(); List<List<Integer>> llist = new LinkedList<List<Integer>>(); if(candidates == null || candidates.length == 0) return llist; java.util.Arrays.sort(candidates); getCombination(0, target, candidates, list, llist); return llist; } public void getCombination(int start, int target, int[] candidates, LinkedList<Integer> list, List<List<Integer>> llist) { for(int i = start; i < candidates.length; i++) { if(target > candidates[i]) { list.add(candidates[i]); getCombination(i, target - candidates[i], candidates, list, llist); list.removeLast(); } else if(target == candidates[i]) { list.add(candidates[i]); if(!llist.contains(list)) llist.add(new LinkedList<Integer>(list)); list.removeLast(); } else { return; } } } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 227Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 226You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 342Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 333Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 451Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 512Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 430Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 620Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 428The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 388Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 519Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 533Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 371All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 861Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 883Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 555Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 601Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 764Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 736You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 629For a undirected graph with tre ...
相关推荐
239 Combination Sum II 579 240 Combination Sum III 581 241 Combinations 583 242 Letter Combinations of a Phone Number 587 243 Restore IP Addresses 589 244 Reverse Integer 591 245 Palindrome Number 593
Combination Sum III Generate Parentheses Sudoku Solver Word Search 总结 分治法 Pow(x,n) Sqrt(x) 贪心法 Jump Game Jump Game II Best Time to Buy and Sell Stock Best Time to Buy and Sell Stock II Longest...
Combination Sum Medium 回溯 0040 Combination Sum II Medium 回溯 0046 Permutations Medium 回溯 0047 Permutations II Medium 递归、回溯 0051 N-Queens Hard 回溯 0053 Maximum Subarray Easy 动态规划 0069 ...
combinationSum ( self , candidates , target ): def backtrack ( tmp , start , end , target ): if target == 0 : ans . append ( tmp [:]) elif target > 0 : for i in range ( start , end ): tmp . append ( ...
leetcode题库 Little Algorithm 从 2020 年初开始,我在公众号《面向大象编程》上发表面试算法、...Combination Sum组合总和 Combination Sum II组合总和 II Permutations全排列 Permutations II全排列 II Maximum Suba
039:Combination Sum 040:Combination Sum II 046:Permutations 047:Permutations II 051:N-Queens 052:N-Queens II 071: Letter Combinations of a Phone Number 093:Restore IP Addresses 树的遍历问题也...
python实现基本算法 所有组合 所有排列 所有子序列 ... Combination Sum Hamiltonian Cycle Knight Tour Minimax Minmax N Queens N Queens Math Rat In Maze Sudoku Sum Of Subsets Word Search
39/Sum.Combination Sum 40/Sum.Combination Sum 2 46.排列(回串) 47/46.排列 2 48.旋转矩阵 49.组字谜 50.(跳过)(坏问题) 53.最大子阵列 54/59.螺旋矩阵 55.跳跃游戏 57.插入间隔 58.(跳过)(Python把戏) 59....
leetcode打不开Leetcode Note Tips Tip1: Two pointer ...Combination Sum II) Tip5: 鸽笼原理要记得,如果题目说要constant extra space,八成就是用input array + swap(#41. First Missing Positive
CombinationSum 组合之和 完成 LinkedList 题目 说明 状态 AddTwoNumber 两数相加 完成 SwapPairs 两两交换链表中的节点 完成 String 题目 说明 状态 LongestSubstring 最长子串 完成 LongestPalindrome 最长回文...
def combinationSum(self, candidates, target): """ :type candidates: List[int] :type target: int :rtype: List[List[int]] """ result,temp = [],[] self.combinationSumRecu(sorted(candidates),...
Leetcode\combination sum(39).swift Leetcode\count number of team(1395).swift Leetcode\counting bits(338).swift Leetcode \find 数组中的所有重复项(442).swift Leetcode\find peak element(162).swift ...
leetcode 2 Useful Links ...Sum], [17 Phone Num] [] BFS [] [] [] DP , [53 max subarray], , [96 DP | BST], , [] [] Binary Search , , [74 2D matrix] [] Slicing Window / Two Pointers [918 Ma
算法 数据结构,算法,动态程序,递归和一点编码的乐趣! 介绍 ...CombinationSum:数组中组合的总和 CompareArrayBST:比较二进制搜索树 DecodeNumber:解码号码的几种方法 FindMiddleShiftedSorted
func combinationSum(candidates []int, target int) (res [][]int) {path := []int{}sort.Ints(candidates)var dfs func(int, int)dfs = func(target, index int) {if target <= 0 {if target == 0 {res = ...
Sum Letter Combination of a Photo Number Palindrome Par 第一节课内容结束,题目训练. 详情 Binary Search Tree (二叉查找树) 第二次课, Binary Search Tree T(n) = O(1) + T( n/2) = O(log n) T(n) = O(1) + ...
combination sum: 39, 40, 216 - palindrome partitioning - regex - sudoku solver: 37 排序 - merge sort - quick sort - insertion sort - selection sort - counting sort 位操作 - find the only element which...
Combination Sum Description 给一组candidates (list of int)和target (int),求使用candidates内数字组合,让总和等于target的所有组合。 candidates内的数字皆不同 candidates内的数字可以重复使用无限次 ...
利用双向链表模拟动态分区分配方式。。。。
leetcode 2 和 c Leetcode_imp_C Leetcode 在 C 上的实现 大批: leetcode_0001_two_sum.c leetcode_0011_max_area.c leetcode_0015_three_sum.c ...leetcode_0039_combination_sum.c 40 leetcode_0041_first_miss