Skip to content

Commit 8c85546

Browse files
committed
matrix traversal
1 parent f41e0d3 commit 8c85546

File tree

5 files changed

+199
-17
lines changed

5 files changed

+199
-17
lines changed

Basic.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,23 @@ Suitable for problems where solutions are within in a range and the tentative so
184184
# Handy code to produce a mapped index, like [0,2,4,1,3,5] or [0,2,4,1,3]
185185
(0..(n+1)/2).map(|x|{2*x}).collect(); // [0,2,4] when n=5 or 6.
186186
(0..n/2).map(|x|{2*x}).collect(); // [1,3] when n=5, or [1,3,5] when n=6.
187-
In [1,3,5,0,4] or [1,3,5,0,2,4], every consecutive three are not adjacent.
187+
In [1,3,0,2,4] or [1,3,5,0,2,4], every consecutive 3 are not adjacent.
188188
```
189189
* [324. Wiggle Sort II](src/problem/p0324_wiggle_sort_ii.rs)
190190
* [767. Reorganize String](src/problem/p0767_reorganize_string.rs)
191191

192192
## Matrix Traversal
193193
### Spiral
194-
### Left Diagonal
195-
### Right Diagonal
194+
* [54. Spiral Matrix](src/problem/p0054_spiral_matrix.rs)
195+
* [885. Spiral Matrix III](src/problem/p0885_spiral_matrix_iii.rs)
196+
### Diagonal
197+
* [498. Diagonal Traverse](src/problem/p0498_diagonal_traverse.rs)
198+
* [1424. Diagonal Traverse II](src/problem/p1424_diagonal_traverse_ii.rs)
199+
200+
## Bit Operation
201+
## 2 Pointer
202+
## Array/String
203+
## DP
196204
## Others
197205
* [42. Trapping Rain Water](src/problem/p0042_trapping_rain_water.rs)
198206
### Palindrome Series

src/problem/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,5 @@ mod p1092_shortest_common_supersequence;
170170
mod p0216_combination_sum_iii;
171171
mod p0212_word_search_ii;
172172
mod p0875_koko_eating_bananas;
173+
mod p0498_diagonal_traverse;
174+
mod p1424_diagonal_traverse_ii;

src/problem/p0054_spiral_matrix.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,49 @@ pub struct Solution {}
3232
impl Solution {
3333
pub fn spiral_order(matrix: Vec<Vec<i32>>) -> Vec<i32> {
3434
let mut result = vec![];
35-
let row_count = matrix.len();
36-
let col_count = matrix[0].len();
35+
let row_count = matrix.len() as i32;
36+
let col_count = matrix[0].len() as i32;
3737

3838
let mut top = 0i32;
3939
let mut bottom = (row_count - 1) as i32;
4040
let mut left = 0i32;
4141
let mut right = (col_count - 1) as i32;
4242

4343
loop {
44-
// println!("A top={}, bottom={}, left={}, right={}", top, bottom, left, right);
4544
for j in left..=right {
4645
result.push(matrix[top as usize][j as usize]);
4746
}
48-
top += 1;
49-
if left > right || top > bottom {
47+
// if left > right || top > bottom {
48+
// break;
49+
// }
50+
if result.len() as i32 == row_count * col_count {
5051
break;
5152
}
53+
top += 1;
5254

53-
// println!("B top={}, bottom={}, left={}, right={}", top, bottom, left, right);
5455
for i in top..=bottom {
5556
result.push(matrix[i as usize][right as usize]);
5657
}
57-
right-=1;
58-
if left > right || top > bottom {break}
58+
if result.len() as i32 == row_count * col_count {
59+
break;
60+
}
61+
right -= 1;
5962

60-
// println!("C top={}, bottom={}, left={}, right={}", top, bottom, left, right);
6163
for j in (left..=right).rev() {
6264
result.push(matrix[bottom as usize][j as usize]);
6365
}
64-
bottom -=1;
65-
if left > right || top > bottom {break}
66+
if result.len() as i32 == row_count * col_count {
67+
break;
68+
}
69+
bottom-=1;
6670

67-
// println!("D top={}, bottom={}, left={}, right={}", top, bottom, left, right);
6871
for i in (top..=bottom).rev() {
6972
result.push(matrix[i as usize][left as usize]);
7073
}
71-
left+=1;
72-
if left > right || top > bottom {break}
74+
if result.len() as i32 == row_count * col_count {
75+
break;
76+
}
77+
left +=1;
7378
}
7479
result
7580
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* [498] Diagonal Traverse
3+
*
4+
* Given an m x n matrix mat, return an array of all the elements of the array in a diagonal order.
5+
*
6+
* Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2021/04/10/diag1-grid.jpg" style="width: 334px; height: 334px;" />
8+
* Input: mat = [[1,2,3],[4,5,6],[7,8,9]]
9+
* Output: [1,2,4,7,5,3,6,8,9]
10+
*
11+
* Example 2:
12+
*
13+
* Input: mat = [[1,2],[3,4]]
14+
* Output: [1,2,3,4]
15+
*
16+
*
17+
* Constraints:
18+
*
19+
* m == mat.length
20+
* n == mat[i].length
21+
* 1 <= m, n <= 10^4
22+
* 1 <= m * n <= 10^4
23+
* -10^5 <= mat[i][j] <= 10^5
24+
*
25+
*/
26+
pub struct Solution {}
27+
28+
// problem: https://leetcode.com/problems/diagonal-traverse/
29+
// discuss: https://leetcode.com/problems/diagonal-traverse/discuss/?currentPage=1&orderBy=most_votes&query=
30+
31+
// submission codes start here
32+
33+
impl Solution {
34+
pub fn valid(i : i32, j : i32, row_count : i32, col_count : i32) -> bool {
35+
0 <= i && i < row_count && 0 <= j && j < col_count
36+
}
37+
pub fn find_diagonal_order(mat: Vec<Vec<i32>>) -> Vec<i32> {
38+
let mut result = vec![];
39+
let row_count : i32 = mat.len() as i32;
40+
let col_count : i32 = mat[0].len() as i32;
41+
let mut i : i32 = 0;
42+
let mut j : i32 = 0;
43+
loop {
44+
while !Self::valid(i, j, row_count, col_count) {
45+
i-=1;
46+
j+=1;
47+
}
48+
while Self::valid(i, j, row_count, col_count) {
49+
result.push(mat[i as usize][j as usize]);
50+
i-=1;
51+
j+=1;
52+
}
53+
j+=1;
54+
if result.len() as i32 == row_count * col_count {
55+
break;
56+
}
57+
58+
while !Self::valid(i, j, row_count, col_count) {
59+
i+=1;
60+
j-=1;
61+
}
62+
while Self::valid(i, j, row_count, col_count) {
63+
result.push(mat[i as usize][j as usize]);
64+
i+=1;
65+
j-=1;
66+
}
67+
j+=1;
68+
if result.len() as i32 == row_count * col_count {
69+
break;
70+
}
71+
72+
}
73+
result
74+
}
75+
}
76+
77+
// submission codes end
78+
79+
#[cfg(test)]
80+
mod tests {
81+
use super::*;
82+
83+
#[test]
84+
fn test_498() {
85+
assert_eq!(Solution::find_diagonal_order(vec![vec![1,2,3],vec![4,5,6],vec![7,8,9]]), vec![1,2,4,7,5,3,6,8,9]);
86+
}
87+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* [1424] Diagonal Traverse II
3+
*
4+
* Given a list of lists of integers, nums, return all elements of nums in diagonal order as shown in the below images.
5+
*
6+
* Example 1:
7+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_1_1784.png" style="width: 158px; height: 143px;" />
8+
*
9+
* Input: nums = [[1,2,3],[4,5,6],[7,8,9]]
10+
* Output: [1,4,2,7,5,3,8,6,9]
11+
*
12+
* Example 2:
13+
* <img alt="" src="https://assets.leetcode.com/uploads/2020/04/08/sample_2_1784.png" style="width: 230px; height: 177px;" />
14+
*
15+
* Input: nums = [[1,2,3,4,5],[6,7],[8],[9,10,11],[12,13,14,15,16]]
16+
* Output: [1,6,2,8,7,3,9,4,12,10,5,13,11,14,15,16]
17+
*
18+
* Example 3:
19+
*
20+
* Input: nums = [[1,2,3],[4],[5,6,7],[8],[9,10,11]]
21+
* Output: [1,4,2,5,3,8,6,9,7,10,11]
22+
*
23+
* Example 4:
24+
*
25+
* Input: nums = [[1,2,3,4,5,6]]
26+
* Output: [1,2,3,4,5,6]
27+
*
28+
*
29+
* Constraints:
30+
*
31+
* 1 <= nums.length <= 10^5
32+
* 1 <= nums[i].length <= 10^5
33+
* 1 <= nums[i][j] <= 10^9
34+
* There at most 10^5 elements in nums.
35+
*
36+
*/
37+
pub struct Solution {}
38+
39+
// problem: https://leetcode.com/problems/diagonal-traverse-ii/
40+
// discuss: https://leetcode.com/problems/diagonal-traverse-ii/discuss/?currentPage=1&orderBy=most_votes&query=
41+
42+
// submission codes start here
43+
44+
impl Solution {
45+
pub fn find_diagonal_order(nums: Vec<Vec<i32>>) -> Vec<i32> {
46+
let mut start_positions : Vec<(i32,i32)> = vec![];
47+
let row_count : usize = nums.len();
48+
let col_count : usize = nums[0].len();
49+
for i in 0..row_count {
50+
start_positions.push((i as i32, 0 as i32));
51+
}
52+
for j in 1..col_count {
53+
start_positions.push((row_count as i32 - 1, j as i32));
54+
}
55+
56+
let mut result : Vec<i32> = vec![];
57+
for start_pos in start_positions.iter() {
58+
let mut i = start_pos.0;
59+
let mut j = start_pos.1;
60+
while 0 <= i && i < row_count as i32 && 0 <=j && j < col_count as i32 {
61+
result.push(nums[i as usize][j as usize]);
62+
i-=1;
63+
j+=1;
64+
}
65+
}
66+
result
67+
}
68+
}
69+
70+
// submission codes end
71+
72+
#[cfg(test)]
73+
mod tests {
74+
use super::*;
75+
76+
#[test]
77+
fn test_1424() {
78+
assert_eq!(Solution::find_diagonal_order(vec![vec![1,2,3],vec![4,5,6],vec![7,8,9]]), vec![1,4,2,7,5,3,8,6,9]);
79+
}
80+
}

0 commit comments

Comments
 (0)