File tree Expand file tree Collapse file tree 2 files changed +83
-0
lines changed Expand file tree Collapse file tree 2 files changed +83
-0
lines changed Original file line number Diff line number Diff line change @@ -302,6 +302,61 @@ func generateMatrix(n int) [][]int {
302
302
}
303
303
```
304
304
305
+ Swift:
306
+
307
+ ``` swift
308
+ func generateMatrix (_ n : Int ) -> [[Int ]] {
309
+ var result = [[Int ]](repeating : [Int ](repeating : 0 , count : n), count : n)
310
+
311
+ var startRow = 0
312
+ var startColumn = 0
313
+ var loopCount = n / 2
314
+ let mid = n / 2
315
+ var count = 1
316
+ var offset = 1
317
+ var row: Int
318
+ var column: Int
319
+
320
+ while loopCount > 0 {
321
+ row = startRow
322
+ column = startColumn
323
+
324
+ for c in column ..< startColumn + n - offset {
325
+ result[startRow][c] = count
326
+ count += 1
327
+ column += 1
328
+ }
329
+
330
+ for r in row ..< startRow + n - offset {
331
+ result[r][column] = count
332
+ count += 1
333
+ row += 1
334
+ }
335
+
336
+ for _ in startColumn ..< column {
337
+ result[row][column] = count
338
+ count += 1
339
+ column -= 1
340
+ }
341
+
342
+ for _ in startRow ..< row {
343
+ result[row][column] = count
344
+ count += 1
345
+ row -= 1
346
+ }
347
+
348
+ startRow += 1
349
+ startColumn += 1
350
+ offset += 2
351
+ loopCount -= 1
352
+ }
353
+
354
+ if (n % 2 ) != 0 {
355
+ result[mid][mid] = count
356
+ }
357
+ return result
358
+ }
359
+ ```
305
360
306
361
307
362
Original file line number Diff line number Diff line change @@ -304,6 +304,34 @@ var removeElements = function(head, val) {
304
304
};
305
305
```
306
306
307
+ Swift:
308
+
309
+ ``` swift
310
+ /**
311
+ * Definition for singly-linked list.
312
+ * public class ListNode {
313
+ * public var val: Int
314
+ * public var next: ListNode?
315
+ * public init() { self.val = 0; self.next = nil; }
316
+ * public init(_ val: Int) { self.val = val; self.next = nil; }
317
+ * public init(_ val: Int, _ next: ListNode?) { self.val = val; self.next = next; }
318
+ * }
319
+ */
320
+ func removeElements (_ head : ListNode? , _ val : Int ) -> ListNode? {
321
+ let dummyNode = ListNode ()
322
+ dummyNode.next = head
323
+ var currentNode = dummyNode
324
+ while let curNext = currentNode.next {
325
+ if curNext.val == val {
326
+ currentNode.next = curNext.next
327
+ } else {
328
+ currentNode = curNext
329
+ }
330
+ }
331
+ return dummyNode.next
332
+ }
333
+ ```
334
+
307
335
308
336
309
337
You can’t perform that action at this time.
0 commit comments