forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DFS] add Solutions to Subsets I, II and Permutations I, II
- Loading branch information
Yi Gu
committed
May 19, 2016
1 parent
8756eee
commit 037226e
Showing
4 changed files
with
151 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/permutations/ | ||
* Primary idea: Classic Depth-first Search, remember backtracking | ||
* | ||
* Time Complexity: O(n!), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class Permutations { | ||
func permute(nums: [Int]) -> [[Int]] { | ||
var res = [[Int]]() | ||
var path = [Int]() | ||
var visited = [Bool](count: nums.count, repeatedValue: false) | ||
|
||
let nums = nums.sort({$0 < $1}) | ||
|
||
_dfs(&res, &path, nums, &visited) | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(inout res: [[Int]], inout _ path: [Int], _ nums: [Int], inout _ visited: [Bool]) { | ||
// termination case | ||
if path.count == nums.count { | ||
let path = path | ||
res.append(path) | ||
return | ||
} | ||
|
||
for i in 0 ..< nums.count { | ||
guard !visited[i] else { | ||
continue | ||
} | ||
|
||
path.append(nums[i]) | ||
visited[i] = true | ||
_dfs(&res, &path, nums, &visited) | ||
visited[i] = false | ||
path.removeLast() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/permutations-ii/ | ||
* Primary idea: Classic Depth-first Search, adopt last occurrence to avoid dupliates | ||
* | ||
* Time Complexity: O(n!), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class PermutationsII { | ||
func permuteUnique(nums: [Int]) -> [[Int]] { | ||
var res = [[Int]]() | ||
var path = [Int]() | ||
var visited = [Bool](count: nums.count, repeatedValue: false) | ||
|
||
let nums = nums.sort({$0 < $1}) | ||
|
||
_dfs(&res, &path, nums, &visited) | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(inout res: [[Int]], inout _ path: [Int], _ nums: [Int], inout _ visited: [Bool]) { | ||
// termination case | ||
if path.count == nums.count { | ||
let path = path | ||
res.append(path) | ||
return | ||
} | ||
|
||
for i in 0 ..< nums.count { | ||
if visited[i] || (i > 0 && nums[i] == nums[i - 1] && visited[i - 1]) { | ||
continue | ||
} | ||
|
||
path.append(nums[i]) | ||
visited[i] = true | ||
_dfs(&res, &path, nums, &visited) | ||
visited[i] = false | ||
path.removeLast() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/subsets/ | ||
* Primary idea: Classic Depth-first Search | ||
* | ||
* Time Complexity: O(n!), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class Subsets { | ||
func subsets(nums: [Int]) -> [[Int]] { | ||
var res = [[Int]]() | ||
var path = [Int]() | ||
|
||
let nums = nums.sort({$0 < $1}) | ||
|
||
_dfs(&res, &path, nums, 0) | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(inout res: [[Int]], inout _ path: [Int], _ nums: [Int], _ index: Int) { | ||
// termination case | ||
let newPath = path | ||
res.append(newPath) | ||
|
||
for i in index ..< nums.count { | ||
path.append(nums[i]) | ||
_dfs(&res, &path, nums, i + 1) | ||
path.removeLast() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/subsets-ii/ | ||
* Primary idea: Classic Depth-first Search, avoid duplicates by adopting the first occurrence | ||
* | ||
* Time Complexity: O(n!), Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class SubsetsII { | ||
func subsetsWithDup(nums: [Int]) -> [[Int]] { | ||
var res = [[Int]]() | ||
var path = [Int]() | ||
|
||
let nums = nums.sort({$0 < $1}) | ||
|
||
_dfs(&res, &path, nums, 0) | ||
|
||
return res | ||
} | ||
|
||
private func _dfs(inout res: [[Int]], inout _ path:[Int], _ nums: [Int], _ index: Int) { | ||
let newPath = path | ||
res.append(newPath) | ||
|
||
for i in index ..< nums.count { | ||
if i > 0 && nums[i] == nums[i - 1] && i != index { | ||
continue | ||
} | ||
|
||
path.append(nums[i]) | ||
_dfs(&res, &path, nums, i + 1) | ||
path.removeLast() | ||
} | ||
} | ||
} |