Skip to content

Commit 594929f

Browse files
authored
feat: add swift solution to lcci problems: No.01.01,01.03 (#2564)
1 parent f16d75a commit 594929f

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

lcci/01.01.Is Unique/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ var isUnique = function (astr) {
128128
};
129129
```
130130

131+
```swift
132+
class Solution {
133+
func isUnique(_ astr: String) -> Bool {
134+
var mask = 0
135+
for c in astr {
136+
let i = Int(c.asciiValue! - Character("a").asciiValue!)
137+
if (mask >> i) & 1 != 0 {
138+
return false
139+
}
140+
mask |= 1 << i
141+
}
142+
return true
143+
}
144+
}
145+
```
146+
131147
<!-- tabs:end -->
132148

133149
<!-- end -->

lcci/01.01.Is Unique/README_EN.md

+16
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,22 @@ var isUnique = function (astr) {
135135
};
136136
```
137137

138+
```swift
139+
class Solution {
140+
func isUnique(_ astr: String) -> Bool {
141+
var mask = 0
142+
for c in astr {
143+
let i = Int(c.asciiValue! - Character("a").asciiValue!)
144+
if (mask >> i) & 1 != 0 {
145+
return false
146+
}
147+
mask |= 1 << i
148+
}
149+
return true
150+
}
151+
}
152+
```
153+
138154
<!-- tabs:end -->
139155

140156
<!-- end -->

lcci/01.01.Is Unique/Solution.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func isUnique(_ astr: String) -> Bool {
3+
var mask = 0
4+
for c in astr {
5+
let i = Int(c.asciiValue! - Character("a").asciiValue!)
6+
if (mask >> i) & 1 != 0 {
7+
return false
8+
}
9+
mask |= 1 << i
10+
}
11+
return true
12+
}
13+
}

lcci/01.03.String to URL/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ var replaceSpaces = function (S, length) {
6666
};
6767
```
6868

69+
```swift
70+
class Solution {
71+
func replaceSpaces(_ S: String, _ length: Int) -> String {
72+
let substring = S.prefix(length)
73+
var result = ""
74+
75+
for character in substring {
76+
if character == " " {
77+
result += "%20"
78+
} else {
79+
result.append(character)
80+
}
81+
}
82+
83+
return result
84+
}
85+
}
86+
```
87+
6988
<!-- tabs:end -->
7089

7190
### 方法二:模拟

lcci/01.03.String to URL/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,25 @@ var replaceSpaces = function (S, length) {
7979
};
8080
```
8181

82+
```swift
83+
class Solution {
84+
func replaceSpaces(_ S: String, _ length: Int) -> String {
85+
let substring = S.prefix(length)
86+
var result = ""
87+
88+
for character in substring {
89+
if character == " " {
90+
result += "%20"
91+
} else {
92+
result.append(character)
93+
}
94+
}
95+
96+
return result
97+
}
98+
}
99+
```
100+
82101
<!-- tabs:end -->
83102

84103
### Solution 2: Simulation
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
func replaceSpaces(_ S: String, _ length: Int) -> String {
3+
let substring = S.prefix(length)
4+
var result = ""
5+
6+
for character in substring {
7+
if character == " " {
8+
result += "%20"
9+
} else {
10+
result.append(character)
11+
}
12+
}
13+
14+
return result
15+
}
16+
}

0 commit comments

Comments
 (0)