由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 如何避免permutation中的重复计数
相关主题
一道面试题和解法(求指点).3sum on LeetCode OJ
permutationII ,如果不用hashset,用迭代的方法,如何防止重复combination sum II
Given a string, find all its permutations without any repetition?java 求助
permuation sequence 超时求个4sum的算法
LeetCode Runtime Error 一问LeetCode 的 4 sum 问题 如何用hash table做呢?
出道题。perfectPermutation请问如何去除结果里面的重复
Exposed上一道string permutation的题请教一下subset I 输出子集顺序问题
leetcode的3sum的运行时间问题4sum o(n^2)超时
相关话题的讨论汇总
话题: num话题: arraylist话题: integer话题: int话题: swap
进入JobHunting版参与讨论
1 (共1页)
a**********0
发帖数: 422
1
忘了哪个网友发过一个帖子 如果备选数字中有重复则不容易避免重复计数(在不使用
hashset一类的额外数据结构的时候)
我觉得可以这样 有个算法 是求下一个permutation 然后你从第一个permutation开始
生成 不断求下一个permutation 则此序列不会有重复
但是这个不是针对combination的
a**********0
发帖数: 422
2
java代码
import java.util.*;
public class PermutationsII {

public ArrayList> permuteUnique(int[] num) {

Arrays.sort(num);
ArrayList> myResult = new ArrayList Integer>>();

ArrayList temp1 = new ArrayList();
for(int i = 0; i<= num.length-1; i++)
temp1.add(num[i]);

myResult.add(temp1);

while(nextPermutation(num)){
ArrayList temp = new ArrayList();
for(int i = 0; i<= num.length-1; i++)
temp.add(num[i]);

myResult.add(temp);
}
return myResult;
}

public boolean nextPermutation(int[] num) {

int k = -1;
int l = -1;
int swap;

for(int i =0; i<= num.length-2; i++){
if(num[i] k = i;
}

if(k == -1){
return false;
}

for(int i =k; i<= num.length-1; i++){
if(num[k] l = i;
}

swap = num[k];
num[k] = num[l];
num[l] = swap;

for(int front = k+1, end = num.length-1; front < end; front ++, end-
-){
swap = num[front];
num[front] = num[end];
num[end] = swap;
}
return true;
}
}

【在 a**********0 的大作中提到】
: 忘了哪个网友发过一个帖子 如果备选数字中有重复则不容易避免重复计数(在不使用
: hashset一类的额外数据结构的时候)
: 我觉得可以这样 有个算法 是求下一个permutation 然后你从第一个permutation开始
: 生成 不断求下一个permutation 则此序列不会有重复
: 但是这个不是针对combination的

1 (共1页)
进入JobHunting版参与讨论
相关主题
4sum o(n^2)超时LeetCode Runtime Error 一问
一道面试题出道题。perfectPermutation
问个Java的HashSet.contains的问题Exposed上一道string permutation的题
Linked电面分享,挺好的题 应该已挂leetcode的3sum的运行时间问题
一道面试题和解法(求指点).3sum on LeetCode OJ
permutationII ,如果不用hashset,用迭代的方法,如何防止重复combination sum II
Given a string, find all its permutations without any repetition?java 求助
permuation sequence 超时求个4sum的算法
相关话题的讨论汇总
话题: num话题: arraylist话题: integer话题: int话题: swap