Skip to content

Commit 6a26931

Browse files
authored
feat: update swift solutions to lc problem: No.01.06、No.01.07 (#2589)
* Swift implementation for 01.06 & 01.07 Signed-off-by: Lanre Adedara * style: format code and docs with prettier --------- Signed-off-by: Lanre Adedara Co-authored-by: klever34 <klever34@users.noreply.github.com>
1 parent 232af91 commit 6a26931

File tree

6 files changed

+132
-0
lines changed

6 files changed

+132
-0
lines changed

lcci/01.06.Compress String/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ var compressString = function (S) {
167167
};
168168
```
169169

170+
```swift
171+
class Solution {
172+
func compressString(_ S: String) -> String {
173+
let n = S.count
174+
var compressed = ""
175+
var i = 0
176+
177+
while i < n {
178+
var j = i
179+
let currentChar = S[S.index(S.startIndex, offsetBy: i)]
180+
while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar {
181+
j += 1
182+
}
183+
compressed += "\(currentChar)\(j - i)"
184+
i = j
185+
}
186+
187+
return compressed.count < n ? compressed : S
188+
}
189+
}
190+
```
191+
170192
<!-- tabs:end -->
171193

172194
<!-- end -->

lcci/01.06.Compress String/README_EN.md

+22
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,28 @@ var compressString = function (S) {
173173
};
174174
```
175175

176+
```swift
177+
class Solution {
178+
func compressString(_ S: String) -> String {
179+
let n = S.count
180+
var compressed = ""
181+
var i = 0
182+
183+
while i < n {
184+
var j = i
185+
let currentChar = S[S.index(S.startIndex, offsetBy: i)]
186+
while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar {
187+
j += 1
188+
}
189+
compressed += "\(currentChar)\(j - i)"
190+
i = j
191+
}
192+
193+
return compressed.count < n ? compressed : S
194+
}
195+
}
196+
```
197+
176198
<!-- tabs:end -->
177199

178200
<!-- end -->
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
func compressString(_ S: String) -> String {
3+
let n = S.count
4+
var compressed = ""
5+
var i = 0
6+
7+
while i < n {
8+
var j = i
9+
let currentChar = S[S.index(S.startIndex, offsetBy: i)]
10+
while j < n && S[S.index(S.startIndex, offsetBy: j)] == currentChar {
11+
j += 1
12+
}
13+
compressed += "\(currentChar)\(j - i)"
14+
i = j
15+
}
16+
17+
return compressed.count < n ? compressed : S
18+
}
19+
}

lcci/01.07.Rotate Matrix/README.md

+24
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,30 @@ public class Solution {
203203
}
204204
```
205205

206+
```swift
207+
class Solution {
208+
func rotate(_ matrix: inout [[Int]]) {
209+
let n = matrix.count
210+
211+
for i in 0..<(n >> 1) {
212+
for j in 0..<n {
213+
let t = matrix[i][j]
214+
matrix[i][j] = matrix[n - i - 1][j]
215+
matrix[n - i - 1][j] = t
216+
}
217+
}
218+
219+
for i in 0..<n {
220+
for j in 0..<i {
221+
let t = matrix[i][j]
222+
matrix[i][j] = matrix[j][i]
223+
matrix[j][i] = t
224+
}
225+
}
226+
}
227+
}
228+
```
229+
206230
<!-- tabs:end -->
207231

208232
<!-- end -->

lcci/01.07.Rotate Matrix/README_EN.md

+24
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,30 @@ public class Solution {
232232
}
233233
```
234234

235+
```swift
236+
class Solution {
237+
func rotate(_ matrix: inout [[Int]]) {
238+
let n = matrix.count
239+
240+
for i in 0..<(n >> 1) {
241+
for j in 0..<n {
242+
let t = matrix[i][j]
243+
matrix[i][j] = matrix[n - i - 1][j]
244+
matrix[n - i - 1][j] = t
245+
}
246+
}
247+
248+
for i in 0..<n {
249+
for j in 0..<i {
250+
let t = matrix[i][j]
251+
matrix[i][j] = matrix[j][i]
252+
matrix[j][i] = t
253+
}
254+
}
255+
}
256+
}
257+
```
258+
235259
<!-- tabs:end -->
236260

237261
<!-- end -->
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
func rotate(_ matrix: inout [[Int]]) {
3+
let n = matrix.count
4+
5+
for i in 0..<(n >> 1) {
6+
for j in 0..<n {
7+
let t = matrix[i][j]
8+
matrix[i][j] = matrix[n - i - 1][j]
9+
matrix[n - i - 1][j] = t
10+
}
11+
}
12+
13+
for i in 0..<n {
14+
for j in 0..<i {
15+
let t = matrix[i][j]
16+
matrix[i][j] = matrix[j][i]
17+
matrix[j][i] = t
18+
}
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)