- 浏览: 137626 次
文章分类
- 全部博客 (189)
- Tree (14)
- Dynamic Programming (34)
- Array (20)
- Search (1)
- Hash (12)
- Backtracking (22)
- Divide and Conque (8)
- Greedy (6)
- Stack (12)
- software (0)
- List (7)
- Math (22)
- Two pointers (16)
- String (20)
- Linux (1)
- Sliding Window (4)
- Finite State Machine (1)
- Breadth-first Search (7)
- Graph (4)
- DFS (6)
- BFS (3)
- Sort (9)
- 基础概念 (2)
- 沟通表达 (0)
- Heap (2)
- Binary Search (15)
- 小结 (1)
- Bit Manipulation (8)
- Union Find (4)
- Topological Sort (1)
- PriorityQueue (1)
- Design Pattern (1)
- Design (1)
- Iterator (1)
- Queue (1)
最新评论
-
likesky3:
看了数据结构书得知并不是迭代和递归的区别,yb君的写法的效果是 ...
Leetcode - Graph Valid Tree -
likesky3:
迭代和递归的区别吧~
Leetcode - Graph Valid Tree -
qb_2008:
还有一种find写法:int find(int p) { i ...
Leetcode - Graph Valid Tree -
qb_2008:
要看懂这些技巧的代码确实比较困难。我是这么看懂的:1. 明白这 ...
Leetcode - Single Num II -
qb_2008:
public int singleNumber2(int[] ...
Leetcode - Single Num II
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]
[分析] 每个元素可以使用任意多次,求解时就为每个元素枚举使用一次、两次直到其最大次数的情况,下面给出两者递归实现方式,recur1代码简洁些,而recur2效率稍高些,因为减少了递归调用次数,可手动模拟下[1,2],target=3的求解过程就能体会。此题最需注意的是for循环体前面的if判断,作用是避免重复结果,在我做时leetcode 是漏掉了这类test case,我有bug的code成了漏网之鱼~ 看到Code Ganker的解析后意识到了,考虑[1,1],1 这个case帮助理解。
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]
[分析] 每个元素可以使用任意多次,求解时就为每个元素枚举使用一次、两次直到其最大次数的情况,下面给出两者递归实现方式,recur1代码简洁些,而recur2效率稍高些,因为减少了递归调用次数,可手动模拟下[1,2],target=3的求解过程就能体会。此题最需注意的是for循环体前面的if判断,作用是避免重复结果,在我做时leetcode 是漏掉了这类test case,我有bug的code成了漏网之鱼~ 看到Code Ganker的解析后意识到了,考虑[1,1],1 这个case帮助理解。
public class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integer>> result = new ArrayList<List<Integer>>(); if (candidates == null) return result; Arrays.sort(candidates); recur1(candidates, target, 0, new ArrayList<Integer>(), result); return result; } public void recur1(int[] candidates, int target, int start, ArrayList<Integer> oneAns, List<List<Integer>> result) { if (target <= 0) { if (target == 0) result.add((ArrayList<Integer>)oneAns.clone()); return; } for (int i = start; i < candidates.length && candidates[i] <= target; i++) { if (i > 0 && candidates[i] == candidates[i - 1]) continue; oneAns.add(candidates[i]); recur(candidates, target - candidates[i], i, oneAns, result); oneAns.remove(oneAns.size() - 1); } } public void recur2(int[] candidates, int target, int start, ArrayList<Integer> oneAns, List<List<Integer>> result) { if (target <= 0) { if (target == 0) result.add((ArrayList<Integer>)oneAns.clone()); return; } for (int i = start; i < candidates.length && candidates[i] <= target; i++) { if (i > 0 && candidates[i] == candidates[i - 1]) continue; int maxTimes = target / candidates[i]; for (int j = 1; j <= maxTimes; j++) { oneAns.add(candidates[i]); recur(candidates, target - j * candidates[i], i + 1, oneAns, result); } for (int j = 1; j <= maxTimes; j++) oneAns.remove(oneAns.size() - 1); } } }
发表评论
-
Leetcode - Palindrome Permutation II
2015-08-28 21:17 2214Given a string s, return all th ... -
Leetcode - Factor Combination
2015-08-28 09:53 862Numbers can be regarded as prod ... -
Leetcode - Generate Parentheses
2015-08-08 17:01 528[分析] 第一个思路(错误的~):假设递归函数返回 n - ... -
Leetcode - Word Search II
2015-08-03 21:25 973iven a 2D board and a list of w ... -
Leetcode - Word Search
2015-08-03 21:03 516Given a 2D board and a word, fi ... -
Leetcode - Subset
2015-08-02 12:06 951[分析] 三种思路 思路1:每层递归新加一个元素,第一层递归, ... -
Leetcode - Subset II
2015-08-02 12:13 957[分析] 延续Subset三种思路,关键是添加去重处理 思路 ... -
Leetcode - Gray Code
2015-08-01 17:26 580原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation Sequence
2015-08-01 17:19 515原题链接:https://leetcode.com/probl ... -
Leetcode - Permutation II
2015-08-01 10:49 604原题链接:https://leetcode.com/probl ... -
Leetcode - Combination
2015-08-01 08:36 496[分析] 从 n 个数中取 k 个数,第一个数有 n 种取法… ... -
Leetcode - Combination Sum III
2015-07-31 22:04 523[分析] 思路就是枚举k个数所有可能的组合并判断是否符合条件。 ... -
Leetcode - Combination Sum II
2015-07-31 21:06 616[分析] 输入数组中的每个元素至多使用一次,相较于Combin ... -
Leetcode - Sudoku Solver
2015-07-31 09:14 467[分析] 做Valid Sudoku时表示3*3区块的下标想得 ... -
Leetcode - N Queues II
2015-07-30 20:52 407[分析] 做完N皇后第一题,这个就so easy~ pu ... -
Leetcode - N-Queens
2015-07-30 20:38 445[分析] N皇后摆放规则:两个皇后不能共存于同一行、同一列以及 ... -
Leetcode - Word Ladder II
2015-06-26 09:19 533Given two words (start and end) ... -
Leetcode - Combination Sum III
2015-06-10 10:09 549Find all possible combinati ... -
Leetcode - Palindrome Partition
2015-05-21 09:56 786Given a string s, partition s s ... -
Leetcode - WordBreak III
2015-04-16 08:30 463Given a string s and a dictio ...
相关推荐
c语言入门 C语言_leetcode题解之39-combination-sum.c
c语言入门 C语言_leetcode题解之40-combination-sum-ii.c
js js_leetcode题解之39-combination-sum.js
js js_leetcode题解之40-combination-sum-II.js
java入门 java_leetcode题解之039_Combination_Sum
python python_leetcode题解之216_Combination_Sum_III.py
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: 找出所有相加之和为特定值的组合。 - First Missing Positive: 找出数组中缺失的最小正数。 - Trapping Rain Water: 给定一个数组,其中每个元素代表一个宽度为1的柱子高度,计算能接多少雨水。 ...
java java_leetcode题解之Combination Sum.java
java java_leetcode题解之Combination Sum IV.java
java java_leetcode题解之Combination Sum III.java
java java_leetcode题解之Combination Sum II.java
9. **模拟和回溯**:对于某些组合优化问题,可以使用回溯法来枚举所有可能的解决方案,例如"组合总和"(Combination Sum)。 10. **排序与堆**:数组的排序问题可以通过快速排序、归并排序等经典算法解决,而"最大...
leetcode怎么销号 LeetCode-Solutions :green_heart:My own LeetCode solutions No. Problem LeetCode 力扣 Python Go Solution Difficulty Tag 0017 Letter Combinations of a Phone Number Medium 回溯、暴力 0034...
leetcode怎么计算空间复杂度是指 LeetCode-Solution my first solution of LeetCode 2015-5-7 Problem 95,98(80 already!) 我经常在递归的结束地方忘记return!!! 题型一:经典暴力递归:(里面涉及到重复不重复的...
4. **回溯**:回溯是一种解决约束满足问题的算法,常用于“组合总和”(Combination Sum)、“N 皇后问题”(N-Queens)等。JavaScript 中回溯算法往往结合递归来实现。 5. **动态规划**:动态规划是解决优化问题的...
第 296 章PY 和 GO 中的 Leetcode 我在 Python Golang ...Leetcode ...40:combination-sum-ii:传递最后选择的索引 41:先缺失正,交换 42:只是提醒:块 - 垃圾箱 43:多字符串,i+j,i+j+1 44:通配符
而回溯法常用于解决组合优化问题,如"Combination Sum"(组合求和),在Java中通常结合递归来实现。 三、深度优先搜索与广度优先搜索 DFS(深度优先搜索)和BFS(广度优先搜索)是图论和树形结构中常用的算法。Java...