由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - String permunation question (CS)
相关主题
请问大牛们leetcode上的Permutations IIPermutation leetcode-
一个容易记忆的permutation算法string permutation,怎么处理重复字母?
Given a string, find all its permutations without any repetition?请教cracking the code interview两题
今天才整明白Permutation的最优解!?两道简单的面试题
求问permutation这个题amazon onsite 面经
Non-recursive permutation问一个题目
一道amazon题谁能贴一下求nth permutation 和已知permutation 求rank的code
Exposed上一道string permutation的题谁能帮我写写这道题? print all permutations of a string
相关话题的讨论汇总
话题: arr话题: num话题: string话题: curr话题: int
进入JobHunting版参与讨论
1 (共1页)
l********r
发帖数: 140
1
Old question, but can't find a good and easy to understand answer.
What happens if there are duplicate letters in the string? like, abcdd.
p*****2
发帖数: 21240
2
You shouldn't output duplicate permutations.
r****c
发帖数: 2585
3
use recursive, fix the first letter then do permutation for the rest n-1.

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

l********r
发帖数: 140
4
It asks to handle the case where there are duplicate letters in the input
string.
s**x
发帖数: 7506
5
use or implement your own std::next_permutation
x*******8
发帖数: 145
6

Easy way is use set, you cannot store duplicate keys in set :D
Or you can do some check, e.g aabc,
if(i>0&&str[i]==str[i-1]&&!flag[i-1]) continue;

【在 l********r 的大作中提到】
: It asks to handle the case where there are duplicate letters in the input
: string.

p*****2
发帖数: 21240
7
p = (x) -> console.log x
swap = (arr, i, j) -> [arr[i], arr[j]] = [arr[j], arr[i]]
dfs = (arr, curr) ->
if curr is arr.length
p arr.join("")
return
set = {}
for i in [curr...arr.length]
unless set[arr[i]]
set[arr[i]]=true
swap arr, curr, i
dfs arr, curr+1
swap arr, curr, i
permutation = (str) -> dfs str.split(""), 0
l*n
发帖数: 529
8
递归并用一个Boolean[26],记录是否用过某个字符作为开头了。

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

h**o
发帖数: 548
9
我用swap那种解法理解的。去duplicate就加上noswap()判断。
bool noswap(int level, int i, const vector& num){
for (int j=level;j if (num[j]==num[i]){
return true;
}
}
return false;
}

void perm_recur(vector &num, int size, int level, vector > >& results) {
if (level == size) {results.push_back(num); return;}
for (int i = level; i < size; i++){
if (noswap(level, i, num)) continue;
swap(num[level], num[i]);
perm_recur(num, size, level+1, results);
swap(num[level], num[i]);
}
}

【在 l********r 的大作中提到】
: Old question, but can't find a good and easy to understand answer.
: What happens if there are duplicate letters in the string? like, abcdd.

1 (共1页)
进入JobHunting版参与讨论
相关主题
谁能帮我写写这道题? print all permutations of a string求问permutation这个题
请教 怎样存下这个stringNon-recursive permutation
T家电面面经并且不解为何被秒拒一道amazon题
求问个G家面试题Exposed上一道string permutation的题
请问大牛们leetcode上的Permutations IIPermutation leetcode-
一个容易记忆的permutation算法string permutation,怎么处理重复字母?
Given a string, find all its permutations without any repetition?请教cracking the code interview两题
今天才整明白Permutation的最优解!?两道简单的面试题
相关话题的讨论汇总
话题: arr话题: num话题: string话题: curr话题: int