File tree 6 files changed +132
-0
lines changed
6 files changed +132
-0
lines changed Original file line number Diff line number Diff line change @@ -167,6 +167,28 @@ var compressString = function (S) {
167
167
};
168
168
```
169
169
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
+
170
192
<!-- tabs: end -->
171
193
172
194
<!-- end -->
Original file line number Diff line number Diff line change @@ -173,6 +173,28 @@ var compressString = function (S) {
173
173
};
174
174
```
175
175
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
+
176
198
<!-- tabs: end -->
177
199
178
200
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -203,6 +203,30 @@ public class Solution {
203
203
}
204
204
```
205
205
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
+
206
230
<!-- tabs: end -->
207
231
208
232
<!-- end -->
Original file line number Diff line number Diff line change @@ -232,6 +232,30 @@ public class Solution {
232
232
}
233
233
```
234
234
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
+
235
259
<!-- tabs: end -->
236
260
237
261
<!-- end -->
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments