File tree Expand file tree Collapse file tree 3 files changed +150
-0
lines changed
lcci/08.14.Boolean Evaluation Expand file tree Collapse file tree 3 files changed +150
-0
lines changed Original file line number Diff line number Diff line change @@ -190,6 +190,57 @@ func countEval(s string, result int) int {
190
190
}
191
191
```
192
192
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
+
193
244
<!-- tabs:end -->
194
245
195
246
<!-- end -->
Original file line number Diff line number Diff line change @@ -201,6 +201,57 @@ func countEval(s string, result int) int {
201
201
}
202
202
```
203
203
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
+
204
255
<!-- tabs:end -->
205
256
206
257
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments