[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 ...
随机推荐
- java注解框架
我们经常会在java代码里面看到:“@Override”,“@Target”等等样子的东西,这些是什么? 在java里面它们是“注解”. 下面是百度百科的解释:java.lang.annotation ...
- c# 其他技术学习
1.注册表编辑 为了方便对注册表进行操作,.NET提供了Registry类和RegistryKey类 2.API函数的应用 (1)自定义特性的代码:在类.属性.方法的上方加上“[]”的代码 (2)有个 ...
- JavaScript Patterns 3.8 Error Objects
The error objects created by constructors(Error(), SyntaxError(), TypeError(), and others) have the ...
- 最短路径之迪杰斯特拉(Dijkstra)算法
迪杰斯特拉(Dijkstra)算法主要是针对没有负值的有向图,求解其中的单一起点到其他顶点的最短路径算法.本文主要总结迪杰斯特拉(Dijkstra)算法的原理和算法流程,最后通过程序实现在一个带权值的 ...
- Spring 通过XML配置文件以及通过注解形式来AOP 来实现前置,环绕,异常通知,返回后通知,后通知
本节主要内容: 一.Spring 通过XML配置文件形式来AOP 来实现前置,环绕,异常通知 1. Spring AOP 前置通知 XML配置使用案例 2. Spring AOP ...
- D_S 顺序栈的基本操作
// main.cpp #include <iostream> using namespace std; #include "Status.h" typedef in ...
- EXCEL IF 函数 模糊查询
A列都是产品名,比如衬衫,长袖衬衫,短袖衬衫,短裙,长裙 搜索A列的产品名,凡是含有“衬衫”的一律在B列对应行输出“衬衫”,凡是含有“裙”字的一律输出“裙子”在B列对应行,请教一下怎么写函数,本来用I ...
- Can't initialize metastore for hive
there maybe many reason to cause this,today our issue is that, if you execute hive –database dbname ...
- uva 1152 4 values whose sum is zero ——yhx
The SUM problem can be formulated as follows: given four lists A;B;C;D of integer values, computehow ...
- 译:Google的大规模集群管理工具Borg(二)------ Borg架构
3.Borg 架构 一个Borg的cell由一系列的机器组成,通常在cell运行着一个逻辑的中央控制器叫做Borgmaster,在cell中的每台机器上则运行着一个叫Borglet的代理进程.而Bor ...