Skip to content

Commit 0b5b5ee

Browse files
author
Yi Gu
committed
Refactor code style to the latest Swift version and fix time & space complexity analysis
1 parent 206855a commit 0b5b5ee

File tree

3 files changed

+23
-33
lines changed

3 files changed

+23
-33
lines changed

Diff for: DFS/Combinations.swift

+8-11
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,27 @@
22
* Question Link: https://leetcode.com/problems/combinations/
33
* Primary idea: Classic Depth-first Search, another version of Subsets
44
*
5-
* Time Complexity: O(n^n), Space Complexity: O(n)
5+
* Time Complexity: O(n * 2^n), Space Complexity: O(n * 2^n)
66
*
77
*/
88

99
class Combinations {
10-
func combine(n: Int, _ k: Int) -> [[Int]] {
11-
var res = [[Int]]()
12-
var path = [Int]()
13-
let nums = [Int](1...n)
10+
func combine(_ n: Int, _ k: Int) -> [[Int]] {
11+
var res = [[Int]](), path = [Int]()
1412

15-
_dfs(nums, &res, &path, 0, k)
13+
dfs(&res, &path, 0, Array(1...n), k)
1614

1715
return res
1816
}
1917

20-
private func _dfs(nums: [Int], inout _ res: [[Int]], inout _ path: [Int], _ index: Int, _ k: Int) {
18+
private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ idx: Int, _ nums: [Int], _ k: Int) {
2119
if path.count == k {
22-
res.append([Int](path))
23-
return
20+
res.append(path)
2421
}
2522

26-
for i in index..<nums.count {
23+
for i in idx..<nums.count {
2724
path.append(nums[i])
28-
_dfs(nums, &res, &path, i + 1, k)
25+
dfs(&res, &path, i + 1, nums, k)
2926
path.removeLast()
3027
}
3128
}

Diff for: DFS/Subsets.swift

+7-12
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,25 @@
22
* Question Link: https://leetcode.com/problems/subsets/
33
* Primary idea: Classic Depth-first Search
44
*
5-
* Time Complexity: O(n^n), Space Complexity: O(n)
5+
* Time Complexity: O(n * 2^n), Space Complexity: O(n * 2^n)
66
*
77
*/
88

99
class Subsets {
10-
func subsets(nums: [Int]) -> [[Int]] {
11-
var res = [[Int]]()
12-
var path = [Int]()
10+
func subsets(_ nums: [Int]) -> [[Int]] {
11+
var res = [[Int]](), path = [Int]()
1312

14-
let nums = nums.sorted(by: <)
15-
16-
_dfs(&res, &path, nums, 0)
13+
dfs(&res, &path, 0, nums)
1714

1815
return res
1916
}
2017

21-
private func _dfs(inout res: [[Int]], inout _ path: [Int], _ nums: [Int], _ index: Int) {
22-
// termination case
23-
18+
private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ idx: Int, _ nums: [Int]) {
2419
res.append(path)
2520

26-
for i in index..<nums.count {
21+
for i in idx..<nums.count {
2722
path.append(nums[i])
28-
_dfs(&res, &path, nums, i + 1)
23+
dfs(&res, &path, i + 1, nums)
2924
path.removeLast()
3025
}
3126
}

Diff for: DFS/SubsetsII.swift

+8-10
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,29 @@
22
* Question Link: https://leetcode.com/problems/subsets-ii/
33
* Primary idea: Classic Depth-first Search, avoid duplicates by adopting the first occurrence
44
*
5-
* Time Complexity: O(n^n), Space Complexity: O(n)
5+
* Time Complexity: O(n * 2^n), Space Complexity: O(n * 2^n)
66
*
77
*/
88

99
class SubsetsII {
10-
func subsetsWithDup(nums: [Int]) -> [[Int]] {
10+
func subsetsWithDup(_ nums: [Int]) -> [[Int]] {
1111
var res = [[Int]](), path = [Int]()
1212

13-
let nums = nums.sorted(by: <)
14-
15-
_dfs(&res, &path, nums, 0)
13+
dfs(&res, &path, 0, nums.sorted())
1614

1715
return res
1816
}
1917

20-
private func _dfs(inout res: [[Int]], inout _ path:[Int], _ nums: [Int], _ index: Int) {
18+
private func dfs(_ res: inout [[Int]], _ path: inout [Int], _ idx: Int, _ nums: [Int]) {
2119
res.append(path)
2220

23-
for i in index..<nums.count {
24-
if i > 0 && nums[i] == nums[i - 1] && i != index {
25-
continue
21+
for i in idx..<nums.count {
22+
if i > 0 && nums[i] == nums[i - 1] && i != idx {
23+
continue
2624
}
2725

2826
path.append(nums[i])
29-
_dfs(&res, &path, nums, i + 1)
27+
dfs(&res, &path, i + 1, nums)
3028
path.removeLast()
3129
}
3230
}

0 commit comments

Comments
 (0)