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 6d6c95a

Browse files
add solution in go: 498. Diagonal Traverse
Signed-off-by: rajput-hemant <rajput.hemant2001@gmail.com>
1 parent bcdb93d commit 6d6c95a

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
type Direction int
4+
5+
const (
6+
Up Direction = iota
7+
Down
8+
)
9+
10+
func findDiagonalOrder(mat [][]int) []int {
11+
direction := Up
12+
row, col := 0, 0
13+
row_len, col_len := len(mat), len(mat[0])
14+
res := make([]int, row_len*col_len)
15+
16+
for i := 0; i < row_len*col_len; i++ {
17+
res[i] = mat[row][col]
18+
19+
if direction == Up {
20+
if row == 0 || col == col_len-1 {
21+
direction = Down
22+
23+
if col == col_len-1 {
24+
row++
25+
} else {
26+
col++
27+
}
28+
29+
} else {
30+
row--
31+
col++
32+
}
33+
} else {
34+
if col == 0 || row == row_len-1 {
35+
direction = Up
36+
37+
if row == row_len-1 {
38+
col++
39+
} else {
40+
row++
41+
}
42+
43+
} else {
44+
row++
45+
col--
46+
}
47+
}
48+
}
49+
50+
return res
51+
}

0 commit comments

Comments
(0)

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