Skip to content

Commit babc029

Browse files
author
Yi Gu
committed
Refactor code to offer better performance and readability
1 parent 12e60ea commit babc029

File tree

2 files changed

+68
-119
lines changed

2 files changed

+68
-119
lines changed

DFS/WordSearchII.swift

+52-105
Original file line numberDiff line numberDiff line change
@@ -11,131 +11,78 @@
1111
class WordSearchII {
1212

1313
func findWords(_ board: [[Character]], _ words: [String]) -> [String] {
14-
var res = [String]()
15-
16-
let m = board.count
17-
let n = board[0].count
18-
19-
let trie = _convertToTrie(words)
20-
var visited = [[Bool]](repeating: Array(repeating: false, count: n), count: m)
21-
22-
for i in 0 ..< m {
23-
for j in 0 ..< n {
24-
_dfs(board, m, n, i, j, &visited, &res, trie, "")
14+
let trie = Trie(words), m = board.count, n = board[0].count
15+
var isVisited = Array(repeating: Array(repeating: false, count: n), count: m), res = Set<String>()
16+
17+
for i in 0..<m {
18+
for j in 0..<n {
19+
search(board, trie, &res, i, j, &isVisited, trie.root, "", m, n)
2520
}
2621
}
27-
28-
return res
22+
23+
return Array(res)
2924
}
3025

31-
fileprivate func _dfs(_ board: [[Character]], _ m: Int, _ n: Int, _ i: Int, _ j: Int, _ visited: inout [[Bool]], _ res: inout [String], _ trie: Trie, _ str: String) {
32-
// beyond matrix
26+
private func search(_ board: [[Character]], _ trie: Trie, _ res: inout Set<String>, _ i: Int, _ j: Int, _ isVisited: inout [[Bool]], _ currentNode: TrieNode, _ currentStr: String, _ m: Int, _ n: Int) {
3327
guard i >= 0 && i < m && j >= 0 && j < n else {
3428
return
3529
}
36-
37-
// check visited
38-
guard !visited[i][j] else {
30+
31+
guard !isVisited[i][j] else {
3932
return
4033
}
41-
42-
// check is word prefix
43-
let str = str + "\(board[i][j])"
44-
guard trie.isWordPrefix(str) else {
34+
35+
guard let child = currentNode.children[board[i][j]] else {
4536
return
4637
}
47-
48-
// check word exist
49-
if trie.isWord(str) && !res.contains(str) {
50-
res.append(str)
38+
39+
isVisited[i][j] = true
40+
41+
let str = currentStr + "\(board[i][j])"
42+
43+
if child.isEnd {
44+
res.insert(str)
5145
}
52-
53-
// check four directions
54-
visited[i][j] = true
55-
_dfs(board, m, n, i + 1, j, &visited, &res, trie, str)
56-
_dfs(board, m, n, i - 1, j, &visited, &res, trie, str)
57-
_dfs(board, m, n, i, j + 1, &visited, &res, trie, str)
58-
_dfs(board, m, n, i, j - 1, &visited, &res, trie, str)
59-
visited[i][j] = false
46+
47+
search(board, trie, &res, i + 1, j, &isVisited, child, str, m, n)
48+
search(board, trie, &res, i - 1, j, &isVisited, child, str, m, n)
49+
search(board, trie, &res, i, j + 1, &isVisited, child, str, m, n)
50+
search(board, trie, &res, i, j - 1, &isVisited, child, str, m, n)
51+
52+
isVisited[i][j] = false
6053
}
6154

62-
func _convertToTrie(_ words: [String]) -> Trie {
63-
let trie = Trie()
64-
65-
for str in words {
66-
trie.insert(str)
55+
class Trie {
56+
var root: TrieNode
57+
58+
init(_ words: [String]) {
59+
root = TrieNode()
60+
61+
words.forEach { insert($0) }
6762
}
68-
69-
return trie
70-
}
71-
}
72-
73-
74-
class Trie {
75-
var root: TrieNode
76-
77-
init() {
78-
root = TrieNode()
79-
}
80-
81-
func insert(_ word: String) {
82-
var node = root
83-
var word = [Character](word.characters)
84-
85-
for i in 0 ..< word.count {
86-
let c = word[i]
87-
88-
if node.children[c] == nil {
89-
node.children[c] = TrieNode()
63+
64+
private func insert(_ word: String) {
65+
var node = root
66+
67+
for char in word {
68+
if node.children[char] == nil {
69+
node.children[char] = TrieNode()
70+
}
71+
72+
node = node.children[char]!
9073
}
91-
92-
node = node.children[c]!
74+
75+
node.isEnd = true
9376
}
94-
95-
node.isEnd = true
9677
}
97-
98-
func isWord(_ word: String) -> Bool {
99-
var node = root
100-
var word = [Character](word.characters)
101-
102-
for i in 0 ..< word.count {
103-
let c = word[i]
104-
105-
if node.children[c] == nil {
106-
return false
107-
}
108-
109-
node = node.children[c]!
110-
}
11178

112-
return node.isEnd
113-
}
79+
class TrieNode {
80+
var isEnd: Bool
81+
var children: [Character: TrieNode]
11482

115-
func isWordPrefix(_ prefix: String) -> Bool {
116-
var node = root
117-
var prefix = [Character](prefix.characters)
118-
119-
for i in 0 ..< prefix.count {
120-
let c = prefix[i]
121-
122-
if node.children[c] == nil {
123-
return false
124-
}
125-
126-
node = node.children[c]!
83+
init() {
84+
isEnd = false
85+
children = [Character: TrieNode]()
12786
}
128-
129-
return true
13087
}
13188
}
132-
133-
class TrieNode {
134-
var isEnd: Bool
135-
var children: [Character:TrieNode]
136-
137-
init() {
138-
isEnd = false
139-
children = [Character:TrieNode]()
140-
}
141-
}

Sort/SortColors.swift

+16-14
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,25 @@
66

77
class SortColors {
88
func sortColors(_ nums: inout [Int]) {
9-
var red = 0, blue = nums.count - 1, i = 0
9+
var redIdx = 0, blueIdx = nums.count - 1, currentIdx = 0
1010

11-
while i <= blue {
12-
if nums[i] == 0 {
13-
_swap(&nums, i, red)
14-
red += 1
15-
i += 1
16-
} else if nums[i] == 1 {
17-
i += 1
18-
} else {
19-
_swap(&nums, i, blue)
20-
blue -= 1
11+
while currentIdx <= blueIdx {
12+
let num = nums[currentIdx]
13+
14+
if num == 0 {
15+
swap(&nums, redIdx, currentIdx)
16+
redIdx += 1
17+
} else if num == 2 {
18+
swap(&nums, currentIdx, blueIdx)
19+
blueIdx -= 1
20+
currentIdx -= 1
2121
}
22+
23+
currentIdx += 1
2224
}
2325
}
2426

25-
fileprivate func _swap<T>(_ nums: inout [T], _ p: Int, _ q: Int) {
26-
(nums[p], nums[q]) = (nums[q], nums[p])
27+
private func swap(_ nums: inout [Int], _ left: Int, _ right: Int) {
28+
(nums[left], nums[right]) = (nums[right], nums[left])
2729
}
28-
}
30+
}

0 commit comments

Comments
 (0)