[Swift]LeetCode491. 遞增子序列 | Increasing Subsequences

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-bkjrrnfg-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .git

Example:github

Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] 

Note:數組

  1. The length of the given array will not exceed 15.
  2. The range of integer in the given array is [-100,100].
  3. The given array may contain duplicates, and two equal integers should also be considered as a special case of increasing sequence.

給定一個整型數組, 你的任務是找到全部該數組的遞增子序列,遞增子序列的長度至少是2。微信

示例:app

輸入: [4, 6, 7, 7]
輸出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

說明:ide

  1. 給定數組的長度不會超過15。
  2. 數組中的整數範圍是 [-100,100]。
  3. 給定數組中可能包含重複數字,相等的數字應該被視爲遞增的一種狀況。

Runtime: 472 ms
Memory Usage: 17.3 MB
 1 class Solution {
 2     func findSubsequences(_ nums: [Int]) -> [[Int]] {
 3         var res:Set<[Int]> = Set<[Int]>()
 4         var cur:[[Int]] = [[]]
 5         for i in 0..<nums.count
 6         {
 7             var n:Int = cur.count
 8             for j in 0..<n
 9             {
10                 if !cur[j].isEmpty && cur[j].last! > nums[i]
11                 {
12                     continue
13                 }
14                 cur.append(cur[j])
15                 cur[cur.count - 1].append(nums[i])
16                 if cur[cur.count - 1].count >= 2
17                 {
18                     res.insert(cur.last!)
19                 }
20             }
21         }
22         return [[Int]](res)
23     }
24 }

 1 class Solution {
 2     func findSubsequences(_ nums: [Int]) -> [[Int]] {
 3         guard nums.count > 0 else { return [[Int]]() }
 4         
 5         var visit = [Bool](repeating: false, count: nums.count)
 6         var res = [[Int]]()
 7         
 8         dfs(nums, 0, &res, &visit, getPrev(nums), [Int]())
 9         
10         return res
11     }
12     
13     private func dfs(_ nums: [Int], _ index: Int, _ res: inout [[Int]], _ visit: inout [Bool], _ prev: [Int], _ list: [Int]) {
14         if list.count > 1 {
15             res.append(list)
16         }
17         
18         var list = list
19         
20         for i in index..<nums.count {
21             if index >= 1, nums[i] < nums[index-1] {
22                 continue
23             }
24             
25             if prev[i] != -1, visit[prev[i]] == false, (list.count == 0 || prev[i] > index-1) {
26                 continue
27             } 
28             
29             list.append(nums[i])
30             visit[i] = true
31             dfs(nums, i+1, &res, &visit, prev, list)
32             visit[i] = false
33             list.removeLast()
34         }
35     }
36     
37     private func getPrev(_ nums: [Int]) -> [Int] {
38         var map = [Int: Int]()
39         var prev = [Int](repeating: -1, count: nums.count)
40         
41         for i in 0..<nums.count {
42             if let value = map[nums[i]] {
43                 prev[i] = value
44             } 
45             map[nums[i]] = i
46         }
47         
48         return prev
49     }
50 }
相關文章
相關標籤/搜索