Skip to content

Commit 38c1aab

Browse files
committed
[Array] Add a solution to Diagonal Traverse
1 parent 9deae0c commit 38c1aab

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

Array/DiagonalTraverse.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/diagonal-traverse/
3+
* Primary idea: use sum to track matrix index, note to set lower and upper bound to avoid duplicates
4+
*
5+
* Time Complexity: O(mn), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class DiagonalTraverse {
10+
func findDiagonalOrder(_ matrix: [[Int]]) -> [Int] {
11+
var res = [Int]()
12+
13+
guard matrix.count > 0 && matrix[0].count > 0 else {
14+
return res
15+
}
16+
17+
let m = matrix.count, n = matrix[0].count
18+
var fromTop = false
19+
20+
for sum in 0...m + n - 2 {
21+
if fromTop {
22+
for i in max(sum - n + 1, 0)...min(m - 1, sum) {
23+
res.append(matrix[i][sum - i])
24+
}
25+
} else {
26+
for i in (max(sum - n + 1, 0)...min(m - 1, sum)).reversed() {
27+
res.append(matrix[i][sum - i])
28+
}
29+
}
30+
31+
fromTop = !fromTop
32+
}
33+
34+
return res
35+
}
36+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* [Microsoft](#microsoft)
2929

3030
## Progress
31-
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 244 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
31+
[Problem Status](#problem-status) shows the latest progress to all 800+ questions. Currently we have 245 completed solutions. Note: questions with ♥ mark means that you have to **Subscript to premium membership** of LeetCode to unlock them. Thank you for great contributions from [CharleneJiang](https://github.com/CharleneJiang), [ReadmeCritic](https://github.com/ReadmeCritic), [demonkoo](https://github.com/demonkoo), [DaiYue](https://github.com/DaiYue), [Quaggie](https://github.com/Quaggie) and [jindulys](https://github.com/jindulys).
3232

3333

3434
## Array
@@ -63,6 +63,7 @@
6363
[Rotate Image](https://leetcode.com/problems/rotate-image/)| [Swift](./Array/RotateImage.swift)| Medium| O(n^2)| O(1)|
6464
[Spiral Matrix](https://leetcode.com/problems/spiral-matrix/)| [Swift](./Array/SpiralMatrix.swift)| Medium| O(n^2)| O(1)|
6565
[Spiral Matrix II](https://leetcode.com/problems/spiral-matrix/)| [Swift](./Array/SpiralMatrixII.swift)| Medium| O(n^2)| O(1)|
66+
[Diagonal Traverse](https://leetcode.com/problems/diagonal-traverse/description/)| [Swift](./Array/DiagonalTraverse.swift)| Medium| O(mn)| O(1)|
6667
[Valid Sudoku](https://leetcode.com/problems/valid-sudoku/)| [Swift](./Array/ValidSudoku.swift)| Easy| O(n^2)| O(n)|
6768
[Set Matrix Zero](https://leetcode.com/problems/set-matrix-zeroes/)| [Swift](./Array/SetMatrixZero.swift)| Medium| O(n^2)| O(1)|
6869
[Next Permutation](https://leetcode.com/problems/next-permutation/)| [Swift](./Array/NextPermutation.swift)| Medium| O(n)| O(1)|

0 commit comments

Comments
 (0)