剑指offer3-排列组合
来源:互联网 发布:js 创建一个json对象 编辑:程序博客网 时间:2024/05/18 02:06
排列:给定一个字符,找出全排列,字符中可能存在重复的字符。
#include<iostream>#include<string>#include<set>using namespace std;void arrangement(string& input, int startIndex, set<string>& res){ if (startIndex == input.size()) { res.insert(input); return; } else { for (int i = startIndex; i < input.size(); i++) { char tmp = input[startIndex]; input[startIndex] = input[i]; input[i] = tmp; //交换两者之间的字符 arrangement(input, startIndex + 1, res); //递归调用 tmp = input[startIndex]; input[startIndex] = input[i]; input[i] = tmp; //字符还原 } }}int main(){ string input; cin >> input; set<string> res; arrangement(input, 0, res); cout << "result:" << endl; for (auto item : res) { cout << item << endl; } return 0;}
组合:给定一个字符,找出组合的结果,字符中可能存在重复的字符。
#include<iostream>#include<algorithm>#include<set>#include<string>using namespace std;void combination(string input, int index, string& tmp, set<string>& res){ if (index == input.size()) { res.insert(tmp); return ; } else { string tmp1 = tmp; combination(input, index + 1, tmp1, res);//当前字符不加入组合 tmp += input[index]; combination(input, index + 1, tmp, res);//当前字符加入组合 }}int main(){ string input; cin >> input; sort(input.begin(), input.end());//将字符进行排序,目的统一字符出现的相对次序 set<string> res;//去重复 string tmp; combination(input, 0, tmp, res); cout << "result:" << endl; for (auto item : res) { cout << item << endl; } return 0;}
0 0
- 剑指offer3-排列组合
- offer3
- 剑指Offer3 二维数据中的查找
- 剑指offer3:二维数组中的查找
- 剑指offer3 从尾到头打印链表
- 数组中的重复(缺失)数字 剑指offer3 及扩展
- 杨氏矩阵的查找(二维数组中的查找——剑指offer3)
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 排列组合
- 牛客网刷题之变态跳台阶
- Java Math.log10()方法
- 43. Multiply Strings
- 64. Minimum Path Sum
- codeforces 505C Mr. Kitayuta, the Treasure Hunter( dp 缩减规模 )
- 剑指offer3-排列组合
- ImportError: cannot import name Process
- POJ 3013 Big Christmas Tree
- qt学习历程 2016.10.05
- Java Math工具类的用法
- [LeetCode]--112. Path Sum
- 无法在 DLL“kernel32”中找到名为“XXX”的入口点
- TankWar 单机(JAVA版) 版本0.3 画出坦克
- Android内核开发 相关工具及源码下载汇总