[Swift]LeetCode46. 全排列 | Permutations
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9907570.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ]
给定一个没有重复数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3] 输出: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ]
1 class Solution { 2 func permute (_ arr:[Int]) -> [[Int]] { 3 4 var array = arr 5 var result = [[Int]]() 6 helper(&array,0,&result) 7 return result 8 } 9 10 func helper (_ arr: inout [Int], _ begin:Int, _ results: inout [[Int]]){ 11 12 if begin >= arr.count{ 13 results.append(arr) 14 return 15 } else { 16 for i in begin..<arr.count{ 17 // choose 18 arr.swapAt(begin, i) 19 // explore 20 helper(&arr, begin + 1, &results) 21 // unchoose 22 arr.swapAt(begin, i) 23 } 24 } 25 } 26 }
16ms
1 class Solution { 2 func permute(_ nums: [Int]) -> [[Int]] { 3 4 var result = [[Int]]() 5 var temp = [Int]() 6 7 var isVisited : [Bool] = Array(repeating: false, count: nums.count) 8 backTracking(nums, &result, &temp, &isVisited) 9 return result 10 11 } 12 13 func backTracking(_ nums: [Int], _ result: inout [[Int]], _ temp: inout [Int], _ isVisited: inout [Bool]) { 14 if temp.count == nums.count { 15 result.append(temp) 16 return 17 } 18 for i in 0 ..< nums.count where !isVisited[i] { 19 temp.append(nums[i]) 20 isVisited[i] = true 21 backTracking(nums, &result, &temp, &isVisited) 22 isVisited[i] = false 23 temp.removeLast() 24 } 25 } 26 }
24ms
1 class Solution { 2 func permute(_ nums: [Int]) -> [[Int]] { 3 var result = [[Int]]() 4 var nums = nums 5 6 func permute(nums: inout [Int], start: Int, end: Int) { 7 if start == end { 8 result.append(nums) 9 } else { 10 for i in start...end { 11 nums.swapAt(i, start) 12 permute(nums: &nums, start: start + 1, end: end) 13 nums.swapAt(i, start) 14 } 15 } 16 } 17 permute(nums: &nums, start: 0, end: nums.count - 1) 18 return result 19 } 20 }
36ms
1 class Solution { 2 func permute(_ nums: [Int]) -> [[Int]] { 3 var result : [[Int]] = [] 4 permuteNext(from: nums, to: [], result: &result) 5 return result 6 } 7 func permuteNext(from : [Int] , to : [Int] , result : inout [[Int]]) { 8 if from.count == 0 { 9 result.append(to) 10 return 11 } 12 13 for idx in 0..<from.count { 14 var newfrom = from 15 newfrom.remove(at: idx) 16 permuteNext(from: newfrom, to: to + [from[idx]], result: &result) 17 } 18 } 19 }
36ms
1 class Solution { 2 var collectList: [[Int]] = [] 3 func permute(_ nums: [Int]) -> [[Int]] { 4 5 if(nums.count == 0) { 6 return collectList 7 } 8 9 permuteDFS(nums, start: 0, tempList: [Int]()) 10 11 return collectList 12 } 13 14 func permuteDFS(_ nums: [Int], start: Int, tempList: [Int]) { 15 16 var temp = tempList 17 18 if(temp.count == nums.count) { 19 20 collectList.append(temp) 21 22 }else { 23 24 for i in 0 ..< nums.count { 25 26 if( temp.contains(nums[i])) { 27 continue 28 } 29 30 temp.append(nums[i]) 31 32 permuteDFS(nums, start: (i + 1), tempList: temp) 33 34 temp.removeLast() 35 } 36 37 } 38 39 } 40 41 }
60ms
1 class Solution { 2 func permute(_ nums: [Int]) -> [[Int]] { 3 if nums.isEmpty { 4 return[[]] 5 } else if nums.count == 1 { 6 return [nums] 7 } else { 8 var result = [[Int]]() 9 for i in 0..<nums.count { 10 var nums = nums 11 let num = nums.remove(at: i) 12 for j in permute(nums) { 13 result.append([num] + j) 14 } 15 } 16 return result 17 } 18 } 19 }
80ms
1 class Solution { 2 func permute(_ nums: [Int]) -> [[Int]] { 3 var result: [[Int]] = [] 4 permute(nums, [], &result) 5 return result 6 } 7 8 func permute(_ nums: [Int], _ nums2: [Int], _ result: inout [[Int]]) { 9 guard nums2.count != nums.count else { 10 result.append(nums2) 11 return 12 } 13 14 nums.filter { !nums2.contains($0) }.forEach { (num) in 15 var nums4 = nums2 16 nums4.append(num) 17 permute(nums, nums4, &result) 18 } 19 } 20 }
转载于:https://www.cnblogs.com/strengthen/p/9907570.html
[Swift]LeetCode46. 全排列 | Permutations相关推荐
- 回溯算法--LeetCode-46 全排列、LeetCode-47 全排列Ⅱ
LeetCode-46 全排列 题目链接:https://leetcode-cn.com/problems/permutations/ 给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输 ...
- Python计算数组的n位全排列(permutations的使用)
计算[1,2,3,4]的3位全排列 from itertools import permutations for i in permutations([1, 2, 3, 4], 3):print('' ...
- Leetcode46全排列DFS
链接 题目大意:给定一个数组,求出所有的全排列. 分析 DFS和回溯的方法. 回溯算法的核心 选择列表:表示当前可做的选择 路径:记录做过的选择 结束条件:遍历到树的底层,在这里是选择列表为空的时候. ...
- leetcode46. 全排列(回溯)
给定一个 没有重复 数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2 ...
- leetcode46.全排列
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1, ...
- 46.全排列(Permutations)
题目描述 给定一个包含不同数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ...
- LeetCode 46. 全排列 Permutations
给定一个没有重复数字的序列,返回其所有可能的全排列. 示例: 输入: [1,2,3] 输出: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1] ] 您是 ...
- c语言实现全排列并存储,C语言实现全排列和回溯法总结
一.递归实现全排列 #include"cstdio" int A[]; void print_permutation(int n,int *A,int cur){ if(cur== ...
- C++STL之next_permutation使用
这是一个计算排列组合的函数 next_permutaion 下一个排列 生成一个字典序更大的排列 Rearranges the elements in the range [first,last) i ...
最新文章
- div实现半透明遮盖层
- 网站优化过度后会出现哪些“后遗症”?悠着点~
- Linux疑难杂症解决方案100篇(十一)-常用Linux命令,助力工作更轻松便捷
- ❗HTML引入CSS的三种常用方式汇总❗
- 数据结构-编程实现一个单链表的测长
- yuv格式转换是那个组织定义的_AI 如何赋能摄像机?这场沙龙为你解锁“软件定义”新概念...
- 使用JUnitParams进行参数化的JUnit测试
- 会议交流|大模型与图学习等知识图谱相关技术实践探索
- mapreduce分组统计_mongodb中使用mapreduce进行分组统计
- odoo pivot中去掉求和_JDK 7 中的 Fork/Join 模式
- properties加载的几种方式
- 计算机专业校企合作实施方案,校企合作-校企合作、工学结合机制实施方案
- 初识window phone 7程序
- 【三维路径规划】基于matlab遗传算法无人机三维路径规划【含Matlab源码 1526期】
- java android rsa加密解密_Android中的RSA加密解密
- Dotween 动态path设置
- C语言示例分析-乒乓球抽取
- 16.【Linux】window和linux下文件格式相互转换
- 手把手教你用Arduino接入阿里云物联网平台,ESP8266连接阿里云物联网平台必看教程...
- 操作系统——文件管理实验
热门文章
- 构建一个ASP.NET Wiki来解释TDD
- mediarecorder 录制的文件无法拖动进度条_录制课程不用愁,小V手把手教学
- vue引入全局静态变量_vue-cli4 全面配置(持续更新)
- 倍数应用题后面需要带单位吗_【小学数学】必考应用题解答思路,多种问题轻松解决!...
- eclipse插件开发(二) 简易4页签编辑器(源码 | 设计 | JS | CSS)配色
- linux rpm安装zabbix,CentOS 7上安装Zabbix Server 3.0 图文详解
- float 精度_为什么float后面要加f
- 怎么样才显示暗=安装好了mysql_linux mysql安装
- menu什么意思中文意思_pipeline什么意思
- 基于python的入侵检测系统毕设_基于深度学习的射频指纹的物联网设备入侵检测...