Skip to content

Commit

Permalink
zigzag debug ing
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxifeng committed Jul 24, 2022
1 parent 129d0a3 commit 7c78c8c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
4 changes: 4 additions & 0 deletions go/cmd/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ var demoCmd = &cobra.Command{
// utf8.RuneCountInString("Hi")

func demoMain() {
// demo.Convert("A", 1)
demo.Convert("ABC", 2)
r := demo.Convert("PAYPALISHIRING", 3)
fmt.Println(r == "PAHNAPLSIIGYIR", r)
fmt.Println(r)
fmt.Println("PAHNAPLSIIGYIR")
}
52 changes: 50 additions & 2 deletions go/demo/leetcode.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package demo

import (
"fmt"
"math/bits"
"unicode/utf8"

Expand Down Expand Up @@ -265,8 +266,55 @@ func LongestPalindrome(s string) string {
func Convert(s string, numRows int) string {
r := []rune(s)
l := len(r)
for i := 0; i < l; i++ {
t := make([]rune, l)

start := numRows + numRows - 2
offset := make([]int, start)

for i, c := 0, 0; i < start; i++ {
if c == 0 {
c = start
}
offset[i] = c
c -= 2
}
fmt.Println(offset)

ti := 0
for row := 0; row < numRows; row++ {
o1, o2 := start-(row*2), row*2

fmt.Println("row ", row, o1, o2)
if row == 0 || row == numRows-1 {
o := Max(o1, o2)
si := row
for i := 0; ; i++ {
si += i * o
if si >= l {
break
}
t[ti] = r[si]
ti++
}
} else {
si := row
for i := 0; ; i += 1 {
si += o1
if si >= l {
break
}
t[ti] = r[si]
ti++
si += o2
if si >= l {
break
}
t[ti] = r[si]
ti++
}
}
}
return string(r)
fmt.Println(ti, ti == l, string(t))

return string(t)
}

0 comments on commit 7c78c8c

Please sign in to comment.