File tree 1 file changed +56
-0
lines changed
1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change
1
+ package leetcode_solutions_golang
2
+
3
+ import (
4
+ "sort"
5
+ "strings"
6
+ )
7
+
8
+ type Item struct {
9
+ row int
10
+ col int
11
+ value string
12
+ }
13
+
14
+ //https://leetcode.com/problems/zigzag-conversion/
15
+ func convert (s string , numRows int ) string {
16
+ curRow := 0
17
+ curCol := 0
18
+ temp := make ([]Item , 0 )
19
+ isDown := true
20
+ for i := 0 ; i < len (s ); i ++ {
21
+ temp = append (temp , Item {
22
+ row : curRow ,
23
+ col : curCol ,
24
+ value : string (s [i ]),
25
+ })
26
+ if isDown {
27
+ curRow ++
28
+ } else {
29
+ curRow --
30
+ curCol ++
31
+ }
32
+ if curRow == numRows - 1 || curRow == 0 {
33
+ isDown = ! isDown
34
+ }
35
+ }
36
+
37
+ sort .Slice (temp , func (i , j int ) bool {
38
+ if temp [i ].row == temp [j ].row {
39
+ if temp [i ].col < temp [j ].col {
40
+ return true
41
+ }
42
+ return false
43
+ }
44
+ if temp [i ].row < temp [j ].row {
45
+ return true
46
+ }
47
+ return false
48
+ })
49
+
50
+ var res strings.Builder
51
+ for i := 0 ; i < len (temp ); i ++ {
52
+ res .WriteString (temp [i ].value )
53
+ }
54
+
55
+ return res .String ()
56
+ }
You can’t perform that action at this time.
0 commit comments