为有牺牲多壮志,敢教日月换新天。

[Swift]LeetCode78. 子集 | Subsets

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9933967.html 
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

热烈欢迎,请直接点击!!!

进入博主App Store主页,下载使用各个作品!!!

注:博主将坚持每月上线一个新app!!!

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。

说明:解集不能包含重复的子集。

示例:

输入: nums = [1,2,3]
输出:
[
  [3],
  [1],
  [2],
  [1,2,3],
  [1,3],
  [2,3],
  [1,2],
  []
]

8ms
 1 class Solution {
 2     func subsets(_ nums: [Int]) -> [[Int]] {
 3         var res = [[Int]()]
 4         subset(&res, nums)
 5         return res
 6     }
 7 
 8     private func subset(_ res: inout [[Int]], _ nums: [Int]) {
 9         guard let n = nums.first else { return }
10         res += res.map { $0 + [n] }
11         subset(&res, Array(nums[1..<nums.count]))
12     }
13 }

12ms

 1 class Solution {
 2     func subsets(_ nums: [Int]) -> [[Int]] {
 3         if nums.count == 0 {
 4             return []
 5         }
 6         
 7         var result = [[Int]]()
 8         var subset = [Int]()
 9         //call backtracking
10         backtracking(nums, &result, &subset, 0)
11         return result
12     }
13     
14     func backtracking(_ nums: [Int], _ result: inout [[Int]], _ subset: inout [Int], _ startIndex: Int) {
15         result.append(subset)
16         for i in startIndex..<nums.count {
17             subset.append(nums[i])
18             backtracking(nums, &result, &subset, i + 1)
19             subset.removeLast()
20         }
21     }
22 }

12ms

 1 class Solution {
 2     func subsets(_ nums: [Int]) -> [[Int]] {
 3         var res = [[Int]]()
 4         rec_subset([Int](), nums, &res)
 5         res.append([Int]())
 6         return res
 7     }
 8     
 9     func rec_subset(_ firstNums: [Int], _ restNum: [Int], _ res: inout [[Int]]) {
10         if restNum.count == 0 {
11             if firstNums.count != 0 {
12                 res.append(firstNums)
13             }
14             return
15         }
16 
17         var tempRestNums = restNum;
18         var tempFirstNums  = firstNums;
19         let firstVal = tempRestNums.remove(at: 0)
20         tempFirstNums.append(firstVal)
21         rec_subset(tempFirstNums, tempRestNums, &res)
22 
23         var zeroFirstNums = firstNums;
24         rec_subset(zeroFirstNums, tempRestNums, &res)
25     }
26 } 

16ms

 1 class Solution {
 2     var result = [[Int]]()
 3     func subsets(_ nums: [Int]) -> [[Int]] {
 4         let count = nums.count
 5         for i in 0...count {
 6             var changeArray = Array(repeatElement(0, count: i))
 7             combine(&changeArray, nums, 0, i, 0, count)
 8         }
 9         return result
10     }
11     
12     func combine(_ combineArray: inout [Int], _ nums: [Int], _ i: Int, _ total: Int, _ j: Int, _ maxJ: Int) {
13         if i == total {
14             result.append(combineArray)
15             return ;
16         }
17         
18         for k in j..<maxJ {
19             combineArray[i] = nums[k]
20             combine(&combineArray, nums, i + 1, total, k + 1, maxJ)
21         }
22     }
23 }

16ms

 1 class Solution {
 2     func subsets(_ nums: [Int]) -> [[Int]] {
 3         guard nums.count > 0 else {return []}
 4         var subsets = [[Int]]()
 5         subsets.append([])
 6         
 7         for num in nums {            
 8             subsets += subsets.map{$0 + [num]}
 9         }
10         return subsets
11     }
12 }

28ms

 1 class Solution {
 2     func subsets(_ nums: [Int]) -> [[Int]] {
 3         if nums.isEmpty {
 4             return []
 5         }
 6         
 7         var res: [[Int]] = [[]]
 8         helper(nums, &res)
 9         return res
10     }
11     
12     fileprivate func helper(_ nums: [Int], _ res: inout [[Int]]) {
13         
14         guard let first = nums.first else {
15             return
16         }
17         
18         res += res.map{$0 + [first]}
19         helper(Array(nums[1..<nums.count]), &res)
20     }
21 }

 

posted @ 2018-11-09 11:08  为敢技术  阅读(407)  评论(0编辑  收藏  举报