Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 68aeac3

Browse files
Merge pull request youngyangyang04#611 from YDLIN/master
添加0059.螺旋矩阵II Swift 版本
2 parents 5001b3d + 59c4a94 commit 68aeac3

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

‎problems/0059.螺旋矩阵II.md‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,61 @@ func generateMatrix(n int) [][]int {
302302
}
303303
```
304304

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+
```
305360

306361

307362

‎problems/0203.移除链表元素.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,34 @@ var removeElements = function(head, val) {
304304
};
305305
```
306306

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+
307335

308336

309337

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /