由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 求教 leetcode上OJ 的Combination Sum II 解法
相关主题
关于结果除掉重复的问题请教请教leetcode Combination Sum II的code,谢谢。
amazon一道面试题a problem from leetcode: high efficiency algorithm for combinations problem
请教两个算法题Interview question: N-sum
一个小题 谁能帮着给点思路 谢谢啦!Combination Sum II哪里做错了
这两道leetcode题有更好的答案吗?hadoop的combiner和partitioner的顺序是什么呢?
a question about combinationCS: print all combination from an array
找硬币的经典问题问个 combination 问题
这题也可以DP 解吧?generate unique integer ID from columns in SQL table
相关话题的讨论汇总
话题: prev话题: sum话题: list话题: dfs
进入JobHunting版参与讨论
1 (共1页)
e***s
发帖数: 799
1
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.
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 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
p*****2
发帖数: 21240
2
def dfs(l, start, list, sum):
if sum==0:
print list
return

if start==len(l) or sum<0:
return

prev=-1
for i in xrange(start,len(l)):
if l[i]!=prev:
list.append(l[i])
dfs(l,i+1,list,sum-l[i])
del list[-1]
prev=l[i]
l=[10,1,2,7,6,1,5]
k=8
l.sort()
list=[]
dfs(l,0,list,k)
e***s
发帖数: 799
3
多谢二哥,请问有没有DP的方法?

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

C***U
发帖数: 2406
4
这个是不是和偷金子的问题一样
是NP的?

.

【在 e***s 的大作中提到】
: 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.
: 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 10,1,2,7,6,1,5 and target 8,
: A solution set is:

p*****2
发帖数: 21240
5

DP不能得到所有结果把?

【在 e***s 的大作中提到】
: 多谢二哥,请问有没有DP的方法?
e***s
发帖数: 799
6
应该可以,但我的办法比较笨,要把index也记住做比较。因为如果只比较数值,
3,2,1,1,4 TARGER: 2
1,1(这个答案就被去掉了)
2

【在 p*****2 的大作中提到】
:
: DP不能得到所有结果把?

p*****2
发帖数: 21240
7

这种题一般不用dp做。除非只要总数,或最优。

【在 e***s 的大作中提到】
: 应该可以,但我的办法比较笨,要把index也记住做比较。因为如果只比较数值,
: 3,2,1,1,4 TARGER: 2
: 1,1(这个答案就被去掉了)
: 2

e***s
发帖数: 799
8
好的,感谢二哥

【在 p*****2 的大作中提到】
:
: 这种题一般不用dp做。除非只要总数,或最优。

y****u
发帖数: 11
9
add
if (l[i] > sum):
break;
to the loop.

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

f*********m
发帖数: 726
10
对二哥的code我有个问题:
若原candidate中有两个1, 比如{1,1, 2, 3},那么用Prev会不会使得1只能被用一次?
比如start=0, l[0]被加到list中去,然后是递归dfs,接着prev被赋值为l[0],然后接着
for loop,此时l[1]和prev比较,因为l[1]也等于1,他等于prev,所以第二个1,也就是
l[1],不会被加到list中去。这样,在最后的结果中只有一个1。但当target=2,输出结
果应该是{1,1},{2}。请指点。
谢谢。

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

相关主题
a question about combination请教leetcode Combination Sum II的code,谢谢。
找硬币的经典问题a problem from leetcode: high efficiency algorithm for combinations problem
这题也可以DP 解吧?Interview question: N-sum
进入JobHunting版参与讨论
S******1
发帖数: 269
11
二哥 is correct.
Using prev is only to prevent using duplicated element in the same level.
For the deeper level, the prev was initially defined as -1, so the deeper
level will use the second 1.

次?

【在 f*********m 的大作中提到】
: 对二哥的code我有个问题:
: 若原candidate中有两个1, 比如{1,1, 2, 3},那么用Prev会不会使得1只能被用一次?
: 比如start=0, l[0]被加到list中去,然后是递归dfs,接着prev被赋值为l[0],然后接着
: for loop,此时l[1]和prev比较,因为l[1]也等于1,他等于prev,所以第二个1,也就是
: l[1],不会被加到list中去。这样,在最后的结果中只有一个1。但当target=2,输出结
: 果应该是{1,1},{2}。请指点。
: 谢谢。

e***s
发帖数: 799
12
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.
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 10,1,2,7,6,1,5 and target 8,
A solution set is:
[1, 7]
[1, 2, 5]
[2, 6]
[1, 1, 6]
p*****2
发帖数: 21240
13
def dfs(l, start, list, sum):
if sum==0:
print list
return

if start==len(l) or sum<0:
return

prev=-1
for i in xrange(start,len(l)):
if l[i]!=prev:
list.append(l[i])
dfs(l,i+1,list,sum-l[i])
del list[-1]
prev=l[i]
l=[10,1,2,7,6,1,5]
k=8
l.sort()
list=[]
dfs(l,0,list,k)
e***s
发帖数: 799
14
多谢二哥,请问有没有DP的方法?

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

C***U
发帖数: 2406
15
这个是不是和偷金子的问题一样
是NP的?

.

【在 e***s 的大作中提到】
: 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.
: 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 10,1,2,7,6,1,5 and target 8,
: A solution set is:

p*****2
发帖数: 21240
16

DP不能得到所有结果把?

【在 e***s 的大作中提到】
: 多谢二哥,请问有没有DP的方法?
e***s
发帖数: 799
17
应该可以,但我的办法比较笨,要把index也记住做比较。因为如果只比较数值,
3,2,1,1,4 TARGER: 2
1,1(这个答案就被去掉了)
2

【在 p*****2 的大作中提到】
:
: DP不能得到所有结果把?

p*****2
发帖数: 21240
18

这种题一般不用dp做。除非只要总数,或最优。

【在 e***s 的大作中提到】
: 应该可以,但我的办法比较笨,要把index也记住做比较。因为如果只比较数值,
: 3,2,1,1,4 TARGER: 2
: 1,1(这个答案就被去掉了)
: 2

e***s
发帖数: 799
19
好的,感谢二哥

【在 p*****2 的大作中提到】
:
: 这种题一般不用dp做。除非只要总数,或最优。

y****u
发帖数: 11
20
add
if (l[i] > sum):
break;
to the loop.

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

相关主题
Combination Sum II哪里做错了问个 combination 问题
hadoop的combiner和partitioner的顺序是什么呢?generate unique integer ID from columns in SQL table
CS: print all combination from an array找工作的感想
进入JobHunting版参与讨论
f*********m
发帖数: 726
21
对二哥的code我有个问题:
若原candidate中有两个1, 比如{1,1, 2, 3},那么用Prev会不会使得1只能被用一次?
比如start=0, l[0]被加到list中去,然后是递归dfs,接着prev被赋值为l[0],然后接着
for loop,此时l[1]和prev比较,因为l[1]也等于1,他等于prev,所以第二个1,也就是
l[1],不会被加到list中去。这样,在最后的结果中只有一个1。但当target=2,输出结
果应该是{1,1},{2}。请指点。
谢谢。

【在 p*****2 的大作中提到】
: def dfs(l, start, list, sum):
: if sum==0:
: print list
: return
:
: if start==len(l) or sum<0:
: return
:
: prev=-1
: for i in xrange(start,len(l)):

S******1
发帖数: 269
22
二哥 is correct.
Using prev is only to prevent using duplicated element in the same level.
For the deeper level, the prev was initially defined as -1, so the deeper
level will use the second 1.

次?

【在 f*********m 的大作中提到】
: 对二哥的code我有个问题:
: 若原candidate中有两个1, 比如{1,1, 2, 3},那么用Prev会不会使得1只能被用一次?
: 比如start=0, l[0]被加到list中去,然后是递归dfs,接着prev被赋值为l[0],然后接着
: for loop,此时l[1]和prev比较,因为l[1]也等于1,他等于prev,所以第二个1,也就是
: l[1],不会被加到list中去。这样,在最后的结果中只有一个1。但当target=2,输出结
: 果应该是{1,1},{2}。请指点。
: 谢谢。

j*******e
发帖数: 1058
23
没java解法么?
c***r
发帖数: 35
24
what's the time complexity here? N power of N?
l****o
发帖数: 315
25
完全背包结果去重
1 (共1页)
进入JobHunting版参与讨论
相关主题
generate unique integer ID from columns in SQL table这两道leetcode题有更好的答案吗?
找工作的感想a question about combination
[合集] 面试算法题一问找硬币的经典问题
[合集] 贡献一道it面试题这题也可以DP 解吧?
关于结果除掉重复的问题请教请教leetcode Combination Sum II的code,谢谢。
amazon一道面试题a problem from leetcode: high efficiency algorithm for combinations problem
请教两个算法题Interview question: N-sum
一个小题 谁能帮着给点思路 谢谢啦!Combination Sum II哪里做错了
相关话题的讨论汇总
话题: prev话题: sum话题: list话题: dfs