由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 请问大牛们leetcode上的Permutations II
相关主题
求问permutation这个题一道amazon题
请教下leetcode Permutations II请教 permute vector of vectors 如何实现,谢谢大家
A Question from leetcode, 求标准解法,本人解的太笨袅amazon onsite 面经
请教leetcode Permutations II 解法和code一个容易记忆的permutation算法
String permunation question (CS)leetcode 的 permutations 一题 oj 怎么不过
leetcode里, backtracking的time complexity怎么算,比如permutations这题目T家电面面经并且不解为何被秒拒
关于排列组合的题目的算法Permutation leetcode-
Given a string, find all its permutations without any repetition?攒rp,发个L家面经
相关话题的讨论汇总
话题: num话题: int话题: vector话题: ret
进入JobHunting版参与讨论
1 (共1页)
a***e
发帖数: 413
1
Given a collection of numbers that might contain duplicates, return all
possible unique permutations.
For example,
[1,1,2] have the following unique permutations:
[1,1,2], [1,2,1], and [2,1,1].
不清楚为什么这个答案会引起Output Limit Exceeded
而把if (i>k&&num[i]==num[k])
改成一个
bool noswap(int k, int i, const vector num){
for (int j=k;j if (num[j]==num[i]){
return true;
}
}
return false;
}
就被accept了。请问如何能提高解决这一类问题的能力呢?感觉工作中很少,几乎用不
到这些东西啊。。。。。以前也没系统学过cs的课。多谢多谢!
vector > permuteUnique(vector &num) {
vector > ret;
int n = num.size();
perm(num,0,n-1,ret);
return ret;
}

void perm(vector num, int k, int n, vector> &ret)
{
if (k==n)
{
ret.push_back(num);
}
else
{
for (int i=k;i<=n;i++)
{
if (i>k&&num[i]==num[k])
continue;
else
{
int t = num[i];
num[i] = num[k];
num[k] = t;

perm(num, k+1, n, ret);

}
}
}
}
T*******e
发帖数: 4928
a***e
发帖数: 413
3
再问一下 , 对于无重复的permutation, 这个答案好理解。但怎么改来处理有重复数
的问题呢?
Given a collection of numbers, return all possible permutations.
For example,
[1,2,3] have the following permutations:
[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].
vector > permute(vector& num) {
sort(num.begin(), num.end());
vector> result;
vector path;
dfs(num, path, result);
return result;
}
private:
void dfs(const vector& num, vector &path,vector > &
result) {
if (path.size() == num.size()) {
result.push_back(path);
return;
}
for (auto i : num) {
auto pos = find(path.begin(), path.end(), i);
if (pos == path.end()) {
path.push_back(i);
dfs(num, path, result);
path.pop_back();
}
}
}
1 (共1页)
进入JobHunting版参与讨论
相关主题
攒rp,发个L家面经String permunation question (CS)
问一道关于字符串的面试题leetcode里, backtracking的time complexity怎么算,比如permutations这题目
Non-recursive permutation关于排列组合的题目的算法
Exposed上一道string permutation的题Given a string, find all its permutations without any repetition?
求问permutation这个题一道amazon题
请教下leetcode Permutations II请教 permute vector of vectors 如何实现,谢谢大家
A Question from leetcode, 求标准解法,本人解的太笨袅amazon onsite 面经
请教leetcode Permutations II 解法和code一个容易记忆的permutation算法
相关话题的讨论汇总
话题: num话题: int话题: vector话题: ret