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, 0, 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 + 1 ,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, 0, 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 = 0; i < s.size(); ++i) {if (!visited[i]) {visited[i] = true;out.push_back(s[i]);getPermsDFS(s, level + 1, visited, out , res);out.pop_back();visited[i] = false;}   }}}
};

解法三:

class Solution {
public:vector<string> getPerms(string s) {if (s.empty()) return vector<string>(1, "");vector<string> res;char first = s[0];string remainder = s.substr(1);vector<string> words = getPerms(remainder);for (auto &a : words) {for (int i = 0; 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(0, i);string end = s.substr(i);return start + c + end;}
};

转载于:https://www.cnblogs.com/grandyang/p/4822859.html

[CareerCup] 9.5 Permutations 全排列相关推荐

  1. [Leetcode] Permutations 全排列

    Permutations I Given a collection of numbers, return all possible permutations. For example, [1,2,3] ...

  2. [LeetCode] Permutations II 全排列之二

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

  3. 【数字全排列】LeetCode 47. Permutations II

    LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...

  4. Leetcode - Permutations I,II

    Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...

  5. 【数字全排列】LeetCode 31. Next Permutation

    LeetCode 31. Next Permutation 参考博客:http://www.cnblogs.com/grandyang/p/4428207.html Solution0:一个偷鸡摸狗的 ...

  6. 面试刷题LeetCode经典100道

    准备面试刷题,100道经典LeetCode题目奉上. 题号 英文题名 中文题名 难度 likes 数 标签 1 Two Sum 两数之和 Easy 11712 数组,哈希表 2 Add Two Num ...

  7. 回溯法,子集选择合集

    框架 回溯的核心特征,做选择. 回溯主要基于递归,它的特点是选择以及撤销选择,非常经典.子问题性质不是很明显.算法复杂度可以从子集数考虑,空间复杂度则一般为递归深度N. 回溯法框架如下: result ...

  8. These Days

    前几天一直窝在宿舍,没有去机房学习.... 昨天去看了后会无期. 今天重回机房. ........................................................... ...

  9. 算法系列——组合总数(Combination Sum)

    题目描述 题目链接:https://leetcode-cn.com/problems/combination-sum/ Given a set of candidate numbers © (with ...

最新文章

  1. C++ 笔记(20)— 异常处理(抛出异常、捕获异常)
  2. bootstrap java_查看tomcat启动文件都干点啥---Bootstrap.java
  3. Javascript模块化编程require.js的用法
  4. git 删除tag_Git 基本应用
  5. springMVC一些实践总结
  6. SQL 2000 中如何 纵表变横表
  7. 超过10%的 Firebase 数据库易受攻击并暴露数据
  8. 如何使用ant_从 0 开始,成为 Ant-Design Contributor
  9. python 小知识总结汇整
  10. google搜索从入门到精通。
  11. python 数据文件上传到ftp服务器
  12. JavaScript第6章上机练习2(使用jQuery美化英雄联盟简介页)上机练习3(制作非缘勿扰页面特效)
  13. Python 算法交易实验41 GMM简单估计
  14. 从技术转管理的困惑(转)
  15. Android屏幕亮度调节
  16. Android设置应用图标
  17. 微信小程序-猜拳小游戏
  18. 设计模式之禅【组合模式】
  19. 用Java编程卫星坐标_2 分钟掌握卫星星座图
  20. shell-环境变量以及环境变量的配置文件

热门文章

  1. 2017年第八届蓝桥杯【C++省赛B组】
  2. 验证码画布生成以及点击图片切换验证码
  3. HDOJ_2010_大二写_水仙花数
  4. 在deepin系统中制作桌面快捷方式
  5. rabbitMQ简单使用
  6. struts2获取服务器临时目录
  7. 单独部署activemq-web-console (转载)
  8. 「洛谷P1343」地震逃生 解题报告
  9. 基本ACL与高级ACL
  10. 在VirtualBox中的Ubuntu中添加新硬盘