[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 ...
随机推荐
- eclipse 中手动安装 subversive SVN
为什么我选择手动安装呢?因为通过 eclipse market 下载实在太慢了. 1.下载离线安装包 http://www.eclipse.org/subversive/latest-releas ...
- 阿里云ECS/Ubuntu下JDK、Tomcat、MySQL安装记录
今天六一儿童节,然后... ... ... ... 然后就是父亲节呀孩子们!!! ———————————————————————割———————————————————————— 同事需要JDK.To ...
- Linux的文件权限
1 文件权限的表示 (1)字母表示法 Linux中所有文件(普通文件.目录文件.字符特殊文件.块特殊文件.管道或FIFO.符号链接.套接字)都有9个权限,如下图所示: -rw-rw-r--就是文件a的 ...
- 团队管理_效率开会[持续更新ing]
1.明确开会目的,这个会议是用来解决什么问题,得出什么结果. 2.明确会议内容与流程,简要说明会议分几个部分,一步一步推进会议的进行. 3.保证参会人员守时参加,会议准时开始. 4.保证会议时间尽量为 ...
- 【API】短信通106端口验证短信的实现
信息时代,无论是电商还是网络营(chuan)销(xiao)都希望得道更多的用户信息.所以很多的网站注册上用到了手机验证码功能.网上有很多的SMS接口提供商.在选择的时候无非就是考虑到1.发送速度:2. ...
- 《SQL Server企业级平台管理实践》读书笔记——当我们的备份都已经损坏的时候该怎么办
作为数据库管理员最最痛苦的莫过于,当数据库宕机的时候需要找备份,但在这个时候突然发现备份文件也是坏的,这就意味着数据会丢失,为此可能会丢掉职位,饭碗不保,所以为此,我们一定要保证好备份的完整性,一般发 ...
- grub条目示例
https://wiki.archlinux.org/index.php/GRUB#Install_to_disk /boot/grub/menu.lst default=0 timeout=5 ti ...
- lvs realserver 配置VIP
# $# 表示提供到shell脚本或者函数的参数总数: # 1表示只有一个参数. #/bin/bash #file: tun_RS.sh if [ $# -ne 1 ]; then echo “usa ...
- Java Consumer and Producer demo
import java.util.Random; import java.util.concurrent.LinkedBlockingQueue; class producer { Rando ...
- 杂谈SharpDx中的WIC组件——我们需要WIC的图片编码功能么?
在前文 SharpDX之Direct2D教程II——加载位图文件和保存位图文件 中,发现在VB2010中不能很好的运用SharpDx中的WIC组件进行图片的编码工作.可能是我的设置问题,也可能是Sha ...