由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一道leetcode上的题目 combination sum
相关主题
问道leetcode的题:Combination Sum II网盘电面一道
关于leetcode的combinationSum题没看懂Leetcode这道题的答案,请指点
a problem from leetcode: high efficiency algorithm for combinations problemcombinations 有没有 iterative的方法阿 ?
一个小题 谁能帮着给点思路 谢谢啦!问个递归的问题
combination sum IICombination Sum 时间和空间复杂度是多少?
发个Palantir的电面,并求g家onsite的bless请教leetcode Combination Sum II的code,谢谢。
question about Leetcode #113 LeetCode – Path Sum II (Java)leetcode的3sum的运行时间问题
请教一道google面试题Combination Sum II哪里做错了
相关话题的讨论汇总
话题: list话题: candidates话题: integer话题: res话题: target
进入JobHunting版参与讨论
1 (共1页)
y*******d
发帖数: 1674
1
LC 39
code 如下
两处不明白
1. 为什么要先排序?
2. 为什么helper(res, target-candidates[i], tmp, candidates, i); 是i不是
i+1??
多谢指点
public List> combinationSum(int[] candidates, int target) {
List> res = new ArrayList>();
if (candidates == null || candidates.length == 0) {
return res;
}
Arrays.sort(candidates);
helper(res, target, new ArrayList(), candidates, 0);

return res;
}

void helper(List> res, int target, List tmp, int[
] candidates, int start) {
if (target == 0) {
res.add(new ArrayList(tmp));
}

for (int i = start; i < candidates.length && target >= candidates[i]
; i++) {
tmp.add(candidates[i]);
helper(res, target-candidates[i], tmp, candidates, i);
tmp.remove(tmp.size()-1);
}
}
e*******s
发帖数: 1979
2
排序reduce search path, 从小到大, 如果当前sum > target 立即返回

【在 y*******d 的大作中提到】
: LC 39
: code 如下
: 两处不明白
: 1. 为什么要先排序?
: 2. 为什么helper(res, target-candidates[i], tmp, candidates, i); 是i不是
: i+1??
: 多谢指点
: public List> combinationSum(int[] candidates, int target) {
: List> res = new ArrayList>();
: if (candidates == null || candidates.length == 0) {

e*******s
发帖数: 1979
3
是i应为每个number可以用多次 参考2, 2, 3 target 7

【在 y*******d 的大作中提到】
: LC 39
: code 如下
: 两处不明白
: 1. 为什么要先排序?
: 2. 为什么helper(res, target-candidates[i], tmp, candidates, i); 是i不是
: i+1??
: 多谢指点
: public List> combinationSum(int[] candidates, int target) {
: List> res = new ArrayList>();
: if (candidates == null || candidates.length == 0) {

y*******d
发帖数: 1674
4
好像不光是减少search path
我试了如果不排序似乎结果也不对

【在 e*******s 的大作中提到】
: 排序reduce search path, 从小到大, 如果当前sum > target 立即返回
e*******s
发帖数: 1979
5
因为排序后面的code依赖于排序假定, 减少了search path.
如果不排序就是full branch DFS, 慢很多而已 不会没结果

【在 y*******d 的大作中提到】
: 好像不光是减少search path
: 我试了如果不排序似乎结果也不对

c********t
发帖数: 5706
6
我觉得leetcode答案和你的code都有问题
题目要求The solution set must not contain duplicate combinations.
你试试
[2,7,3,2,7,2]
7
排序是为了去重,去重的部分你缺了啊。
从i开始是为了能重复用一个元素 (有点绕,就是我自己这个元素想用多少次都可以,
但不能再用和我相同的另一个元素)

【在 y*******d 的大作中提到】
: LC 39
: code 如下
: 两处不明白
: 1. 为什么要先排序?
: 2. 为什么helper(res, target-candidates[i], tmp, candidates, i); 是i不是
: i+1??
: 多谢指点
: public List> combinationSum(int[] candidates, int target) {
: List> res = new ArrayList>();
: if (candidates == null || candidates.length == 0) {

c********t
发帖数: 5706
7
对,也有这个作用。不过排不排序,如果sum>target都要返回,因为all numbers are
positive. 不同的是中间的循环,可以提前跳出。
还有不排序也可以,只要把输入用hashset去重。

【在 e*******s 的大作中提到】
: 排序reduce search path, 从小到大, 如果当前sum > target 立即返回
1 (共1页)
进入JobHunting版参与讨论
相关主题
Combination Sum II哪里做错了combination sum II
CS: print all combination from an array发个Palantir的电面,并求g家onsite的bless
问个fb onsite题目question about Leetcode #113 LeetCode – Path Sum II (Java)
问一个java generic的问题请教一道google面试题
问道leetcode的题:Combination Sum II网盘电面一道
关于leetcode的combinationSum题没看懂Leetcode这道题的答案,请指点
a problem from leetcode: high efficiency algorithm for combinations problemcombinations 有没有 iterative的方法阿 ?
一个小题 谁能帮着给点思路 谢谢啦!问个递归的问题
相关话题的讨论汇总
话题: list话题: candidates话题: integer话题: res话题: target