当前位置:网站首页>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));
}
}
}
};
边栏推荐
- BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
- 个人博客系统项目测试
- Use baidu EasyDL implement factory workers smoking behavior recognition
- Chengdu openGauss user group recruit!
- The Paddle Open Source Community Quarterly Report is here, everything you want to know is here
- 2022-08-01 安装mysql监控工具phhMyAdmin
- 2022-08-01 mysql/stoonedb slow SQL-Q18 analysis
- Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
- Effects of Scraping and Aggregation
- Safety (1)
猜你喜欢
![[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games](/img/8a/07ca69c6dcc22757156cb615e241f8.png)
[Unity entry plan] 2D Game Kit: A preliminary understanding of the composition of 2D games

列表常用方法

FOFAHUB使用测试

BioVendor人俱乐部细胞蛋白(CC16)Elisa试剂盒研究领域
![[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree](/img/b9/35813ae2972375fa728e3c11fab5d3.png)
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree

oracle query scan full table and walk index

LeetCode brush diary: LCP 03. Machine's adventure

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles

ofstream,ifstream,fstream read and write files

Handwritten Blog Platform ~ Day Two
随机推荐
From 2023 onwards, these regions will be able to obtain a certificate with a score lower than 45 in the soft examination.
MySQL8 download, start, configure, verify
NIO‘s Sword(牛客多校赛)
2022-08-01 反思
2022-07-30 mysql8 executes slow SQL-Q17 analysis
openGauss切换后state状态显示不对
局部敏感哈希:如何在常数时间内搜索Embedding最近邻
oracle查询扫描全表和走索引
记一次gorm事务及调试解决mysql死锁
拼多多借力消博会推动国内农产品品牌升级 看齐国际精品农货
Centos7 install postgresql and enable remote access
用位运算为你的程序加速
Redis Subscription and Redis Stream
工程师如何对待开源
Coding Experience Talk
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!
2022-08-01 安装mysql监控工具phhMyAdmin
Project Background Technology Express
BI - SQL 丨 WHILE
Nanoprobes免疫测定丨FluoroNanogold试剂免疫染色方案