由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - CS: print all combination from an array
相关主题
请教一道面试题generate unique integer ID from columns in SQL table
a problem from leetcode: high efficiency algorithm for combinations problem请教一道题
combination sum II请教一道google面试题
请教leetcode Combination Sum II的code,谢谢。发个Palantir的电面,并求g家onsite的bless
Combination Sum II哪里做错了问道leetcode的题:Combination Sum II
这题也可以DP 解吧?问一道leetcode上的题目 combination sum
combinations 有没有 iterative的方法阿 ?A家的题
问个递归的问题一道面试题
相关话题的讨论汇总
话题: index话题: data话题: integer话题: arraylist
进入JobHunting版参与讨论
1 (共1页)
l********r
发帖数: 140
1
Given an array (assume numbers, no duplicate),
print all its combinations.
My code clearly generates duplicates. :-(
public static void combination(ArrayList data)
{
combination(data, 0);
}
private static void combination(ArrayList data, int index)
{
if (index >= data.size())
return;
printArrayUpToIndex(data, index);
for (int i=index; i {
swap(data, index, i);
combination(data, index+1);
}
}
l*****a
发帖数: 14598
2
you need to call swap again to go back to original status
for (int i=index; i {
swap(data, index, i);
combination(data, index+1);
==>
}

【在 l********r 的大作中提到】
: Given an array (assume numbers, no duplicate),
: print all its combinations.
: My code clearly generates duplicates. :-(
: public static void combination(ArrayList data)
: {
: combination(data, 0);
: }
: private static void combination(ArrayList data, int index)
: {
: if (index >= data.size())

l********r
发帖数: 140
3
Still didn't work.
private static void combination(ArrayList data, int index)
{
if (index >= data.size())
return;
for (int i=index; i {
swap(data, index, i);
printArrayUpToIndex(data, index); // print from item 0 up to
item index
combination(data, index+1);
swap(data, i, index);
}
}
For input 1, 2, 3
Somehow the out put is;
[]
[1]
[12]
[1]
[13]
[]
[2]
[21]
[2]
[23]
[]
[3]
[32]
[3]
[31]
1 (共1页)
进入JobHunting版参与讨论
相关主题
一道面试题Combination Sum II哪里做错了
问一道面世题这题也可以DP 解吧?
算法题:两列找共同元素有O(n)的算法吗?combinations 有没有 iterative的方法阿 ?
关于结果除掉重复的问题请教问个递归的问题
请教一道面试题generate unique integer ID from columns in SQL table
a problem from leetcode: high efficiency algorithm for combinations problem请教一道题
combination sum II请教一道google面试题
请教leetcode Combination Sum II的code,谢谢。发个Palantir的电面,并求g家onsite的bless
相关话题的讨论汇总
话题: index话题: data话题: integer话题: arraylist