Skip to content

Commit 2b94218

Browse files
authored
feat: add swift implementation to lcci problem: No.05.01 (doocs#2665)
1 parent 9d76d65 commit 2b94218

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

Diff for: lcci/05.01.Insert Into Bits/README.md

+14
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ function insertBits(N: number, M: number, i: number, j: number): number {
8181
}
8282
```
8383

84+
```swift
85+
class Solution {
86+
func insertBits(_ N: Int, _ M: Int, _ i: Int, _ j: Int) -> Int {
87+
var result = N
88+
89+
for k in i...j {
90+
result &= ~(1 << k)
91+
}
92+
93+
return result | (M << i)
94+
}
95+
}
96+
```
97+
8498
<!-- tabs:end -->
8599

86100
<!-- end -->

Diff for: lcci/05.01.Insert Into Bits/README_EN.md

+14
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ function insertBits(N: number, M: number, i: number, j: number): number {
8181
}
8282
```
8383

84+
```swift
85+
class Solution {
86+
func insertBits(_ N: Int, _ M: Int, _ i: Int, _ j: Int) -> Int {
87+
var result = N
88+
89+
for k in i...j {
90+
result &= ~(1 << k)
91+
}
92+
93+
return result | (M << i)
94+
}
95+
}
96+
```
97+
8498
<!-- tabs:end -->
8599

86100
<!-- end -->

Diff for: lcci/05.01.Insert Into Bits/Solution.swift

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution {
2+
func insertBits(_ N: Int, _ M: Int, _ i: Int, _ j: Int) -> Int {
3+
var result = N
4+
5+
for k in i...j {
6+
result &= ~(1 << k)
7+
}
8+
9+
return result | (M << i)
10+
}
11+
}

0 commit comments

Comments
 (0)