[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, 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 全排列相关推荐
- [Leetcode] Permutations 全排列
Permutations I Given a collection of numbers, return all possible permutations. For example, [1,2,3] ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- 【数字全排列】LeetCode 47. Permutations II
LeetCode 47. Permutations II Solution1:我的答案 笨蛋方法:和第46题的思路差不多,把vector换成了set就OK啦~~~ class Solution { p ...
- Leetcode - Permutations I,II
Leetcode - 046 Permutations 全排列问题是回溯的典型例题: 1.可行解的组成形式是给定数组中的所有数的组合,故而大小上可以作为可行解判定条件 2.每次需要在剩下可被选中的集合 ...
- 【数字全排列】LeetCode 31. Next Permutation
LeetCode 31. Next Permutation 参考博客:http://www.cnblogs.com/grandyang/p/4428207.html Solution0:一个偷鸡摸狗的 ...
- 面试刷题LeetCode经典100道
准备面试刷题,100道经典LeetCode题目奉上. 题号 英文题名 中文题名 难度 likes 数 标签 1 Two Sum 两数之和 Easy 11712 数组,哈希表 2 Add Two Num ...
- 回溯法,子集选择合集
框架 回溯的核心特征,做选择. 回溯主要基于递归,它的特点是选择以及撤销选择,非常经典.子问题性质不是很明显.算法复杂度可以从子集数考虑,空间复杂度则一般为递归深度N. 回溯法框架如下: result ...
- These Days
前几天一直窝在宿舍,没有去机房学习.... 昨天去看了后会无期. 今天重回机房. ........................................................... ...
- 算法系列——组合总数(Combination Sum)
题目描述 题目链接:https://leetcode-cn.com/problems/combination-sum/ Given a set of candidate numbers © (with ...
最新文章
- C++ 笔记(20)— 异常处理(抛出异常、捕获异常)
- bootstrap java_查看tomcat启动文件都干点啥---Bootstrap.java
- Javascript模块化编程require.js的用法
- git 删除tag_Git 基本应用
- springMVC一些实践总结
- SQL 2000 中如何 纵表变横表
- 超过10%的 Firebase 数据库易受攻击并暴露数据
- 如何使用ant_从 0 开始,成为 Ant-Design Contributor
- python 小知识总结汇整
- google搜索从入门到精通。
- python 数据文件上传到ftp服务器
- JavaScript第6章上机练习2(使用jQuery美化英雄联盟简介页)上机练习3(制作非缘勿扰页面特效)
- Python 算法交易实验41 GMM简单估计
- 从技术转管理的困惑(转)
- Android屏幕亮度调节
- Android设置应用图标
- 微信小程序-猜拳小游戏
- 设计模式之禅【组合模式】
- 用Java编程卫星坐标_2 分钟掌握卫星星座图
- shell-环境变量以及环境变量的配置文件