当前位置:网站首页>51. 数字排列
51. 数字排列
2022-08-02 02:20:00 【Hunter_Kevin】
51. 数字排列
输入一组数字(可能包含重复数字),输出其所有的排列方式。
数据范围
输入数组长度 [0,6]。
样例
输入:[1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
代码:
如果数组数字可能有重复,则先对数组元素排序,规定相等元素的相对的先后顺序即可
class Solution {
public:
vector<vector<int>> res;//返回的结果
vector<int> path;//存储当前的一次序列
vector<vector<int>> permutation(vector<int>& nums) {
path.resize(nums.size());//重置大小
sort(nums.begin(), nums.end());//排序
dfs(nums,0,0,0);//从0开始放,记录上一个元素放的位置
return res;
}
// cur 当前需要插入的位置
// start 重复字符这次可以开始的位置
// 用二进制数2^(n-1)标记是否已经使用过
void dfs(vector<int>& nums, int cur, int start, int state){
if(cur >= nums.size()){
res.push_back(path);
return;
}
if(cur == 0 || nums[cur] != nums[cur-1]) start = 0;//可以从下标为0开始插入
for(int i = start; i < nums.size(); i++){
if(!(state>>i&1)){
//如果第i位为0,即没有使用过
// state += 1 << i;//如果放在这里,会影响到下一次循环时state的值
path[i] = nums[cur];
dfs(nums,cur+1,i+1, state + (1 << i));
}
}
}
};
边栏推荐
- FOFAHUB usage test
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- C language inserted into the characters of simple exercises
- 【LeetCode Daily Question】——704. Binary Search
- Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles
- Handwritten Blog Platform ~ Day Two
- 十字光标太小怎么调节、CAD梦想画图算量技巧
- "NetEase Internship" Weekly Diary (2)
- 项目后台技术Express
- BI-SQL丨WHILE
猜你喜欢
Win Go development kit installation configuration, GoLand configuration
Chengdu openGauss user group recruit!
GTK RGB图像绘制
使用DBeaver进行mysql数据备份与恢复
AWR分析报告问题求助:SQL如何可以从哪几个方面优化?
Remember a gorm transaction and debug to solve mysql deadlock
2022-07-30 mysql8执行慢SQL-Q17分析
Service discovery of kubernetes
Outsourcing worked for three years, it was abolished...
【LeetCode每日一题】——654.最大二叉树
随机推荐
oracle query scan full table and walk index
2022-08-01 mysql/stoonedb慢SQL-Q18分析
永磁同步电机36问(二)——机械量与电物理量如何转化?
ofstream,ifstream,fstream read and write files
Simple example of libcurl accessing url saved as file
菜刀webshell特征分析
CodeTon Round 2 D. Magical Array 规律
力扣、752-打开转盘锁
CodeTon Round 2 D. Magical Array
2022-08-01 安装mysql监控工具phhMyAdmin
AI目标分割能力,无需绿幕即可实现快速视频抠图
十字光标太小怎么调节、CAD梦想画图算量技巧
BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
Use baidu EasyDL implement factory workers smoking behavior recognition
NIO's Sword
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
swift项目,sqlcipher3 -&gt; 4,无法打开旧版数据库有办法解决吗
Project Background Technology Express
接口测试神器Apifox究竟有多香?