Skip to content

Commit 22b7dc4

Browse files
authored
feat: add swift implementation to lcci problem: No.17.01 (doocs#2771)
1 parent 1fa7e17 commit 22b7dc4

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

lcci/17.01.Add Without Plus/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,26 @@ class Solution {
4343
}
4444
```
4545

46+
```swift
47+
class Solution {
48+
func add(_ a: Int, _ b: Int) -> Int {
49+
var a = a
50+
var b = b
51+
var sum = 0
52+
var carry = 0
53+
54+
while b != 0 {
55+
sum = a ^ b
56+
carry = (a & b) << 1
57+
a = sum
58+
b = carry
59+
}
60+
61+
return a
62+
}
63+
}
64+
```
65+
4666
<!-- tabs:end -->
4767

4868
<!-- end -->

lcci/17.01.Add Without Plus/README_EN.md

+20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ class Solution {
4444
}
4545
```
4646

47+
```swift
48+
class Solution {
49+
func add(_ a: Int, _ b: Int) -> Int {
50+
var a = a
51+
var b = b
52+
var sum = 0
53+
var carry = 0
54+
55+
while b != 0 {
56+
sum = a ^ b
57+
carry = (a & b) << 1
58+
a = sum
59+
b = carry
60+
}
61+
62+
return a
63+
}
64+
}
65+
```
66+
4767
<!-- tabs:end -->
4868

4969
<!-- end -->
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func add(_ a: Int, _ b: Int) -> Int {
3+
var a = a
4+
var b = b
5+
var sum = 0
6+
var carry = 0
7+
8+
while b != 0 {
9+
sum = a ^ b
10+
carry = (a & b) << 1
11+
a = sum
12+
b = carry
13+
}
14+
15+
return a
16+
}
17+
}

0 commit comments

Comments
 (0)