由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - a problem from leetcode: high efficiency algorithm for combinations problem
相关主题
combinations 有没有 iterative的方法阿 ?combination sum II
Combination Sum II哪里做错了请教一道google面试题
问一道leetcode上的题目 combination sum发个Palantir的电面,并求g家onsite的bless
问个递归的问题问道leetcode的题:Combination Sum II
请教leetcode Combination Sum II的code,谢谢。请教一个题目
CS: print all combination from an array请教leetcode Subsets II
请问一个java的问题(leetcode subsets一题)请教一下subset I 输出子集顺序问题
问个算法题2Facebook Phone Inteview + 流程请教
相关话题的讨论汇总
话题: arraylist话题: integer话题: subset话题: int
进入JobHunting版参与讨论
1 (共1页)
l**********1
发帖数: 415
1
Given two integers n and k, return all possible combinations of k numbers
out of 1 ... n.
For example,
If n = 4 and k = 2, a solution is:
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
the given api is
public ArrayList> combine(int n, int k)
the solution I got is as follows which is really slow O(n!) and time out at
10,7. So please provide any idea of higher efficiency algorithm for this
one. Thanks in advance
public void combination(ArrayList> result,ArrayList<
Integer> subset,int n, int k){
if(k>n) return;
if(k==0){
Collections.sort(subset);
if(!result.contains(subset))
result.add(subset);
return;
}
for(int i=1;i<=n;i++){
ArrayList newset=new ArrayList();
newset.addAll(subset);
if(!newset.contains(i)){
newset.add(i);
combination(result,newset,n,k-1);
}
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
Facebook Phone Inteview + 流程请教请教leetcode Combination Sum II的code,谢谢。
这题也可以DP 解吧?CS: print all combination from an array
关于结果除掉重复的问题请教请问一个java的问题(leetcode subsets一题)
常见编程面试题答案的2种格式,哪种最好?问个算法题2
combinations 有没有 iterative的方法阿 ?combination sum II
Combination Sum II哪里做错了请教一道google面试题
问一道leetcode上的题目 combination sum发个Palantir的电面,并求g家onsite的bless
问个递归的问题问道leetcode的题:Combination Sum II
相关话题的讨论汇总
话题: arraylist话题: integer话题: subset话题: int