Skip to content

Commit

Permalink
[DFS] add Solutions to Subsets I, II and Permutations I, II
Browse files Browse the repository at this point in the history
  • Loading branch information
Yi Gu committed May 19, 2016
1 parent 8756eee commit 037226e
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
42 changes: 42 additions & 0 deletions DFS/Permutations.swift
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()
}
}
}
42 changes: 42 additions & 0 deletions DFS/PermutationsII.swift
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()
}
}
}
32 changes: 32 additions & 0 deletions DFS/Subsets.swift
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()
}
}
}
35 changes: 35 additions & 0 deletions DFS/SubsetsII.swift
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()
}
}
}

0 comments on commit 037226e

Please sign in to comment.