forked from yuyongwei/Algorithms-In-Swift
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdiagonalTraverse.swift
More file actions
76 lines (65 loc) · 1.89 KB
/
Copy pathdiagonalTraverse.swift
File metadata and controls
76 lines (65 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// diagonalTraverse.swift
//
//
// Created by Dong, Anyuan (133) on 2019/4/5.
//
import Foundation
/*
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
https://leetcode.com/problems/diagonal-traverse/
*/
class Solution {
func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] {
guard !matrix.isEmpty else {
return []
}
guard matrix.count > 1 && matrix[0].count > 1 else {
if matrix.count == 1 {
return matrix[0]
}
if matrix[0].count == 1 {
return matrix.flatMap { return $0 }
}
return []
}
var x = 0
var y = 0
let row = matrix.count
let col = matrix[0].count
var result = [matrix[0][0]]
var upside = false
//i: number of diagonal
for _ in 1...(row + col - 3) {
if upside {
if x + 1 >= row {
y = y + 1
} else {
x = x + 1
}
while x >= 1 && y < col - 1 {
result.append(matrix[x][y])
x -= 1
y += 1
}
result.append(matrix[x][y])
} else {
if y + 1 >= col {
x = x + 1
} else {
y = y + 1
}
while y >= 1 && x < row - 1 {
result.append(matrix[x][y])
x += 1
y -= 1
}
result.append(matrix[x][y])
}
upside = !upside
}
//append last
result.append(matrix[row-1][col-1])
return result
}
}