Skip to content

Commit 659f685

Browse files
authored
feat: add swift implementation to lcci problem: No.08.14 (doocs#2706)
1 parent a13b1e8 commit 659f685

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

lcci/08.14.Boolean Evaluation/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,57 @@ func countEval(s string, result int) int {
190190
}
191191
```
192192

193+
```swift
194+
class Solution {
195+
private var memo = [String: [Int]]()
196+
197+
func countEval(_ s: String, _ result: Int) -> Int {
198+
memo = [:]
199+
let ans = dfs(s)
200+
return result == 0 || result == 1 ? ans[result] : 0
201+
}
202+
203+
private func dfs(_ s: String) -> [Int] {
204+
if let res = memo[s] {
205+
return res
206+
}
207+
208+
var res = [0, 0]
209+
if s.count == 1 {
210+
res[Int(String(s))!] = 1
211+
return res
212+
}
213+
214+
for k in 0..<s.count {
215+
let index = s.index(s.startIndex, offsetBy: k)
216+
let op = String(s[index])
217+
218+
if op == "&" || op == "|" || op == "^" {
219+
let left = dfs(String(s[s.startIndex..<index]))
220+
let right = dfs(String(s[s.index(after: index)...]))
221+
222+
for i in 0...1 {
223+
for j in 0...1 {
224+
var v = 0
225+
if op == "&" {
226+
v = i & j
227+
} else if op == "|" {
228+
v = i | j
229+
} else if op == "^" {
230+
v = i ^ j
231+
}
232+
res[v] += left[i] * right[j]
233+
}
234+
}
235+
}
236+
}
237+
238+
memo[s] = res
239+
return res
240+
}
241+
}
242+
```
243+
193244
<!-- tabs:end -->
194245

195246
<!-- end -->

lcci/08.14.Boolean Evaluation/README_EN.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,57 @@ func countEval(s string, result int) int {
201201
}
202202
```
203203

204+
```swift
205+
class Solution {
206+
private var memo = [String: [Int]]()
207+
208+
func countEval(_ s: String, _ result: Int) -> Int {
209+
memo = [:]
210+
let ans = dfs(s)
211+
return result == 0 || result == 1 ? ans[result] : 0
212+
}
213+
214+
private func dfs(_ s: String) -> [Int] {
215+
if let res = memo[s] {
216+
return res
217+
}
218+
219+
var res = [0, 0]
220+
if s.count == 1 {
221+
res[Int(String(s))!] = 1
222+
return res
223+
}
224+
225+
for k in 0..<s.count {
226+
let index = s.index(s.startIndex, offsetBy: k)
227+
let op = String(s[index])
228+
229+
if op == "&" || op == "|" || op == "^" {
230+
let left = dfs(String(s[s.startIndex..<index]))
231+
let right = dfs(String(s[s.index(after: index)...]))
232+
233+
for i in 0...1 {
234+
for j in 0...1 {
235+
var v = 0
236+
if op == "&" {
237+
v = i & j
238+
} else if op == "|" {
239+
v = i | j
240+
} else if op == "^" {
241+
v = i ^ j
242+
}
243+
res[v] += left[i] * right[j]
244+
}
245+
}
246+
}
247+
}
248+
249+
memo[s] = res
250+
return res
251+
}
252+
}
253+
```
254+
204255
<!-- tabs:end -->
205256

206257
<!-- end -->
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
class Solution {
2+
private var memo = [String: [Int]]()
3+
4+
func countEval(_ s: String, _ result: Int) -> Int {
5+
memo = [:]
6+
let ans = dfs(s)
7+
return result == 0 || result == 1 ? ans[result] : 0
8+
}
9+
10+
private func dfs(_ s: String) -> [Int] {
11+
if let res = memo[s] {
12+
return res
13+
}
14+
15+
var res = [0, 0]
16+
if s.count == 1 {
17+
res[Int(String(s))!] = 1
18+
return res
19+
}
20+
21+
for k in 0..<s.count {
22+
let index = s.index(s.startIndex, offsetBy: k)
23+
let op = String(s[index])
24+
25+
if op == "&" || op == "|" || op == "^" {
26+
let left = dfs(String(s[s.startIndex..<index]))
27+
let right = dfs(String(s[s.index(after: index)...]))
28+
29+
for i in 0...1 {
30+
for j in 0...1 {
31+
var v = 0
32+
if op == "&" {
33+
v = i & j
34+
} else if op == "|" {
35+
v = i | j
36+
} else if op == "^" {
37+
v = i ^ j
38+
}
39+
res[v] += left[i] * right[j]
40+
}
41+
}
42+
}
43+
}
44+
45+
memo[s] = res
46+
return res
47+
}
48+
}

0 commit comments

Comments
 (0)