Skip to content

Commit ff8583d

Browse files
authored
feat: add swift implementation to lcp problem: No.17 (#3753)
1 parent 87fbe5d commit ff8583d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

lcp/LCP 17. 速算机器人/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ func calculate(s string) int {
118118
}
119119
```
120120

121+
#### Swift
122+
123+
```swift
124+
class Solution {
125+
func calculate(_ s: String) -> Int {
126+
var x = 1
127+
var y = 0
128+
for c in s {
129+
if c == "A" {
130+
x = x * 2 + y
131+
} else if c == "B" {
132+
y = y * 2 + x
133+
}
134+
}
135+
return x + y
136+
}
137+
}
138+
```
139+
121140
<!-- tabs:end -->
122141

123142
<!-- solution:end -->
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
func calculate(_ s: String) -> Int {
3+
var x = 1
4+
var y = 0
5+
for c in s {
6+
if c == "A" {
7+
x = x * 2 + y
8+
} else if c == "B" {
9+
y = y * 2 + x
10+
}
11+
}
12+
return x + y
13+
}
14+
}

0 commit comments

Comments
 (0)