[CareerCup] 9.5 Permutations 全排列
9.5 Write a method to compute all permutations of a string.
LeetCode上的原题,请参加我之前的博客Permutations 全排列和Permutations II 全排列之二。
解法一:
class Solution {
public:
vector<string> getPerms(string &s) {
vector<string> res;
getPermsDFS(s, , res);
return res;
}
void getPermsDFS(string &s, int level, vector<string> &res) {
if (level == s.size()) res.push_back(s);
else {
for (int i = level; i < s.size(); ++i) {
swap(s[level], s[i]);
getPermsDFS(s, level + ,res);
swap(s[level], s[i]);
}
}
}
};
解法二:
class Solution {
public:
vector<string> getPerms(string s) {
vector<string> res;
vector<bool> visited(s.size(), false);
getPermsDFS(s, , visited, "", res);
return res;
}
void getPermsDFS(string s, int level, vector<bool> &visited, string out, vector<string> &res) {
if (level == s.size()) res.push_back(out);
else {
for (int i = ; i < s.size(); ++i) {
if (!visited[i]) {
visited[i] = true;
out.push_back(s[i]);
getPermsDFS(s, level + , visited, out , res);
out.pop_back();
visited[i] = false;
}
}
}
}
};
解法三:
class Solution {
public:
vector<string> getPerms(string s) {
if (s.empty()) return vector<string>(, "");
vector<string> res;
char first = s[];
string remainder = s.substr();
vector<string> words = getPerms(remainder);
for (auto &a : words) {
for (int i = ; i <= a.size(); ++i) {
string out = insertCharAt(a, first, i);
res.push_back(out);
}
}
return res;
}
string insertCharAt(string s, char c, int i) {
string start = s.substr(, i);
string end = s.substr(i);
return start + c + end;
}
};
[CareerCup] 9.5 Permutations 全排列的更多相关文章
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- [LeetCode] 46. Permutations 全排列
Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- [leetcode]46. Permutations全排列(给定序列无重复元素)
Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...
- [leetcode]47. Permutations全排列(给定序列有重复元素)
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 46. Permutations (全排列)
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- 046 Permutations 全排列
给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
- 46 Permutations(全排列Medium)
题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...
- permutations(全排列)
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
随机推荐
- javascript 全局变量 局部变量 var 与不加var的区别
一.外部的为全局,内部的为局部变量. 二.加var为局部变量(在方法内),不加var为全局变量(当方法内有一次使用后) <script type="text/javascrip ...
- 转 Java多线程中Sleep与Wait的区别
Java中的多线程是一种抢占式的机制,而不是分时机制.抢占式的机制是有多个线程处于可运行状态,但是只有一个线程在运行. 共同点: 1. 他们都是在多线程的环境下,都可以在程序的调用处阻塞指定的毫秒数, ...
- 【windows环境下】RabbitMq的安装和监控插件安装
RabbitMq的安装: RabbitMQ是基于Erlang的,所以必须先配置Erlang环境. 下载Erlang,地址:http://www.erlang.org/download/otp_win3 ...
- cocos2d-x之首选项数据初试
bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...
- QT的QWebView显示网页不全
最近使用QWebView控件遇到一个问题,就是无论窗口多大,网页都显示那么大,而且,显示不完全,有滚动条 试过使用showMaximized()方法, 还是一样,网上一直说是布局问题,也没说清楚是虾米 ...
- 计算几何 平面最近点对 nlogn分治算法 求平面中距离最近的两点
平面最近点对,即平面中距离最近的两点 分治算法: int SOLVE(int left,int right)//求解点集中区间[left,right]中的最近点对 { double ans; //an ...
- 清空select内容
select1.options.length= 0 ; 清空 <ul id="ul"> <li>111</li> <li>111&l ...
- Legendre polynomials
In mathematics, Legendre functions are solutions to Legendre's differential equation: In particular, ...
- NOIP2010提高组 关押罪犯 -SilverN
(洛谷P1525) 题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”( ...
- [转]Javascript中的自执行函数表达式
[转]Javascript中的自执行函数表达式 本文转载自:http://www.ghugo.com/javascript-auto-run-function/ 以下是正文: Posted on 20 ...