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 全排列的更多相关文章

  1. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  2. [LeetCode] 46. Permutations 全排列

    Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3] O ...

  3. lintcode 中等题:permutations 全排列

    题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...

  4. [leetcode]46. Permutations全排列(给定序列无重复元素)

    Given a collection of distinct integers, return all possible permutations. Input: [1,2,3] Output: [ ...

  5. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  6. 46. Permutations (全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  7. 046 Permutations 全排列

    给定一个含有不同数字的集合,返回所有可能的全排列.比如,[1,2,3] 具有如下排列:[  [1,2,3],  [1,3,2],  [2,1,3],  [2,3,1],  [3,1,2],  [3,2 ...

  8. 46 Permutations(全排列Medium)

    题目意思:全排列 思路:其实看这题目意思,是不太希望用递归的,不过还是用了递归,非递归的以后再搞吧 ps:vector这玩意不能随便返回,开始递归方法用vector,直接到500ms,换成void,到 ...

  9. permutations(全排列)

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

随机推荐

  1. MySQL学习基础 之 起航篇

    MySQL 学习来自慕课网<与MySQL的零距离接触> MySQL是一个开源的关系型数据库管理系统 MySQL分为社区版和企业版 MySQL登录和退出相关的命令 参数 描述 -D,--da ...

  2. 团队交流合作简单解决方案:TeamViewer远程控制&会议演示 + HyperCam屏幕录制(免费)

    一. 教程摘要 做开发,团队合作是少不了的.而在合作中,有一部分是花在交流讨论上,其中包括初期的任务分配,成员的进度汇报,以及资源和心得分享等.该教程介绍了两个免费的软件,搭配起来,适合人数不超过25 ...

  3. JavaScript Patterns 2.9 Coding Conventions

    It’s important to establish and follow coding conventions—they make your code consistent, predictabl ...

  4. Python基本语法初试

    编程环境: win7旗舰版 Python 3.2.2(default, Sep  4 2011,09:51:08) 代码来源:(Python菜鸟) 代码内容: Python基本的输出语句print(& ...

  5. redis unwatch discard

    UNWATCH UNWATCH 取消 WATCH 命令对所有 key 的监视. 如果在执行 WATCH 命令之后, EXEC 命令或 DISCARD 命令先被执行了的话,那么就不需要再执行UNWATC ...

  6. hadoop data 相关开源项目(近期学习计划)

    计划学习几个hadoop相关的开源项目: 1.spring hadoop 2.spring batch 3.spring redis 4.spring mongo 相关项目样例:https://git ...

  7. c++仿函数 functor

    内容整理自国外C++教材 先考虑一个简单的例子:假设有一个vector<string>,你的任务是统计长度小于5的string的个数,如果使用count_if函数的话,你的代码可能长成这样 ...

  8. win8程序开机自启动管理

    主要介绍利用系统自身的工具来管理开机自启动,而非第三方的工具,自己了解了,也写出来分享给大家@.·.@ 1.程序设置开机自启动 a. 打开计算机资源管理器-->进入"C:\Progra ...

  9. [linux]SSH公钥登录

    由于口令密码容易泄露,SSH公钥登录相比口令登录更加安全.SSH可以轻松使用非对称加密技术给两台机子订立契约,步骤如下: 第一步 本地机生成秘钥对 指令:ssh-keygen 功能:在本地(~/.ss ...

  10. HDU 4082 Hou Yi's secret --枚举

    题意: 给n个点,问最多有多少个相似三角形(三个角对应相等). 解法: O(n^3)枚举点,形成三角形,然后记录三个角,最后按三个角度依次排个序,算一下最多有多少个连续相等的三元组就可以了. 注意:在 ...