博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ:Permutations(排列)
阅读量:7121 次
发布时间:2019-06-28

本文共 1298 字,大约阅读时间需要 4 分钟。

Given a collection of numbers, return all possible permutations.

For example,

[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1].

排列组合的问题,不用考虑是否发生重复的情况,代码如下:

1 class Solution { 2 public: 3     vector
> permute(vector
& nums) { 4 memset(mark, 0, sizeof(mark)); 5 memset(cdds, 0, sizeof(cdds)); 6 dfs(0, nums.size(), nums); 7 return ret; 8 } 9 10 void dfs(int dep, int maxDep, vector
& nums)11 {12 if(dep == maxDep){13 tmp.clear();14 for(int i = 0;i < maxDep; ++i)15 tmp.push_back(cdds[i]);16 ret.push_back(tmp);17 }else{18 for(int i = 0; i < maxDep; ++i){19 if(!mark[i]){20 mark[i] = true;21 cdds[dep] = nums[i];22 dfs(dep + 1, maxDep, nums);23 mark[i] = false;24 }25 }26 }27 }29 private:30 int cdds[100];31 bool mark[100];32 vector
tmp;33 vector
> ret;34 35 };

 

转载于:https://www.cnblogs.com/-wang-cheng/p/4935641.html

你可能感兴趣的文章
Linux也使用多线程下载
查看>>
用脚本管理服务器日志
查看>>
JBoss企业级应用服务平台群集指南(一)
查看>>
使用高速通道加速iOS版本审核
查看>>
学习笔记:App-V测试错误代码4505CD-1690150A-20000194
查看>>
分布式实时分析数据库citus数据插入性能优化
查看>>
比较好玩的动态添加网页元素
查看>>
关于bacula网络备份软件的安装以及配置1
查看>>
对adapter的封装优化
查看>>
efs解密-Advanced EFS Data Recovery2.1-含注册KEY
查看>>
java运行环境(JRE)
查看>>
安装System Center 2012 R2 数据库
查看>>
iOS 分组索引和索引分区
查看>>
Apache+php 在windows下的配置
查看>>
求二叉树的深度
查看>>
PostFix邮件网关无法向公网投递邮件问题分析
查看>>
可替代的C语言开发环境
查看>>
无任何网络提供程序接受指定的网络路径解决方法
查看>>
XenDesktop 5之痛---Database Transaction Log速增
查看>>
DB2计划三招“破甲” IBM在华能否得偿所愿
查看>>