由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode 的 permutations 一题 oj 怎么不过
相关主题
Exposed上一道string permutation的题T家电面面经并且不解为何被秒拒
关于排列组合的题目的算法请问大牛们leetcode上的Permutations II
Given a string, find all its permutations without any repetition?leetcode里, backtracking的time complexity怎么算,比如permutations这题目
请教 permute vector of vectors 如何实现,谢谢大家Non-recursive permutation
今天才整明白Permutation的最优解!?一道amazon题
谁能帮我写写这道题? print all permutations of a string这两道leetcode题有更好的答案吗?
A Question from leetcode, 求标准解法,本人解的太笨袅youtube, tripadvisor的onsite面经
请教leetcode Permutations II 解法和code用 c 实现的字符串 permutation,求批评指点
相关话题的讨论汇总
话题: vector话题: int话题: it1话题: it2话题: num
进入JobHunting版参与讨论
1 (共1页)
p****o
发帖数: 46
1
oj 在{0, 1}测试的结果是{{1}, {0,1}, {1,0}}
但我自己运行打印出来是{{0,1}, {1,0}}
请大伙帮忙看看。
代码如下
#include "stdio.h"
#include
using namespace std;
class Solution {
public:
vector > permute(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector v(0);
doPermute(v, num);
return vv;
}

void doPermute(vector &v, vector &num) {
if(num.empty()) {
vv.push_back(v);
return;
} else {
for (int i = 0; i < num.size(); i++)
{
int toAdd = num[i];
vector rest(num);
rest.erase(rest.begin()+i);
vector prefix(v);
prefix.push_back(toAdd);
doPermute(prefix, rest);
}
}
}

vector > vv;
};
int main(){
Solution s;

// oj fails with following test case
int x[2]={0, 1};
vector vt(x, x + sizeof x / sizeof x[0]);
s.permute(vt);
// print out results
typedef vector >::iterator VVit;
typedef vector::iterator Vit;
for (VVit it1=s.vv.begin(); it1!=s.vv.end(); ++it1) {
for (Vit it2=it1->begin(); it2!=it1->end(); ++it2) {
printf("value is %d n", *it2);
}
}
}
g****o
发帖数: 547
2
clear vv first
leetcode反复调用的

【在 p****o 的大作中提到】
: oj 在{0, 1}测试的结果是{{1}, {0,1}, {1,0}}
: 但我自己运行打印出来是{{0,1}, {1,0}}
: 请大伙帮忙看看。
: 代码如下
: #include "stdio.h"
: #include
: using namespace std;
: class Solution {
: public:
: vector > permute(vector &num) {

p****o
发帖数: 46
3
多谢多谢。现在过了,我对leetcode还不熟,刚开始,看来还得多到板上啊。

【在 g****o 的大作中提到】
: clear vv first
: leetcode反复调用的

1 (共1页)
进入JobHunting版参与讨论
相关主题
用 c 实现的字符串 permutation,求批评指点今天才整明白Permutation的最优解!?
菜鸟的问题:Given a string, find whether it has any permutation of another string谁能帮我写写这道题? print all permutations of a string
yelp一题,攒rpA Question from leetcode, 求标准解法,本人解的太笨袅
一道onsite面试题请教leetcode Permutations II 解法和code
Exposed上一道string permutation的题T家电面面经并且不解为何被秒拒
关于排列组合的题目的算法请问大牛们leetcode上的Permutations II
Given a string, find all its permutations without any repetition?leetcode里, backtracking的time complexity怎么算,比如permutations这题目
请教 permute vector of vectors 如何实现,谢谢大家Non-recursive permutation
相关话题的讨论汇总
话题: vector话题: int话题: it1话题: it2话题: num