This commit is contained in:
ehlxr 2022-06-20 20:26:14 +08:00
parent e754ded095
commit 774700a83c
5 changed files with 46 additions and 23 deletions

14
main.go
View File

@ -3,23 +3,9 @@ package main
import "fmt" import "fmt"
func main() { func main() {
// node5 := &ListNode{Val: 5, Next: nil}
// node4 := &ListNode{Val: 4, Next: node5}
// node3 := &ListNode{Val: 3, Next: node4}
// node2 := &ListNode{Val: 2, Next: node3}
// node1 := &ListNode{Val: 1, Next: node2}
// root := &ListNode{Val: 0, Next: node1}
// fmt.Printf("%v\n", root)
//
// fmt.Printf("%v\n", Reverse(root))
// fmt.Printf("%v\n", ReverseRec(root))
// fmt.Printf("%v\n", ReverseKGroup(root, 2))
s := "Hello 你好" s := "Hello 你好"
fmt.Println(s[2], s[6]) fmt.Println(s[2], s[6])
rs := []rune(s) rs := []rune(s)
println(string(rs[6])) println(string(rs[6]))
// s := "babad"
// println(LongestPalindrome(s))
} }

View File

@ -1,7 +1,7 @@
package src package src
/* 给你一个字符串 s找到 s 中最长的回文子串 */ /* 给你一个字符串 s找到 s 中最长的回文子串 */
func LongestPalindrome(s string) string { func longestPalindrome(s string) string {
var res string var res string
for i := 0; i < len(s); i++ { for i := 0; i < len(s); i++ {
// 回文串长度为奇数 // 回文串长度为奇数

View File

@ -7,5 +7,5 @@ import (
func TestLongestPalindrome(t *testing.T) { func TestLongestPalindrome(t *testing.T) {
s := "babad" s := "babad"
fmt.Printf("LongestPalindrome of %s is %s\n", s, LongestPalindrome(s)) fmt.Printf("LongestPalindrome of %s is %s\n", s, longestPalindrome(s))
} }

View File

@ -1,6 +1,6 @@
package src package src
func Reverse(root *ListNode) *ListNode { func reverse(root *ListNode) *ListNode {
var pre, tmp *ListNode var pre, tmp *ListNode
cur := root cur := root
for cur != nil { for cur != nil {
@ -14,12 +14,12 @@ func Reverse(root *ListNode) *ListNode {
} }
/* 递归反转链表 */ /* 递归反转链表 */
func ReverseRec(root *ListNode) *ListNode { func reverseRec(root *ListNode) *ListNode {
if root == nil || root.Next == nil { if root == nil || root.Next == nil {
return root return root
} }
res := ReverseRec(root.Next) res := reverseRec(root.Next)
root.Next.Next = root root.Next.Next = root
root.Next = nil root.Next = nil
@ -27,7 +27,7 @@ func ReverseRec(root *ListNode) *ListNode {
} }
/* K 个一组翻转链表 */ /* K 个一组翻转链表 */
func ReverseKGroup(root *ListNode, k int) *ListNode { func reverseKGroup(root *ListNode, k int) *ListNode {
if root == nil { if root == nil {
return root return root
} }
@ -38,13 +38,13 @@ func ReverseKGroup(root *ListNode, k int) *ListNode {
} }
n = n.Next n = n.Next
} }
tmp := ReverseN(root, n) tmp := reverseN(root, n)
root.Next = ReverseKGroup(n, k) root.Next = reverseKGroup(n, k)
return tmp return tmp
} }
/* 反转链表到指定节点 */ /* 反转链表到指定节点 */
func ReverseN(root *ListNode, n *ListNode) *ListNode { func reverseN(root *ListNode, n *ListNode) *ListNode {
var pre, tmp *ListNode var pre, tmp *ListNode
cur := root cur := root
for cur != n { for cur != n {

37
src/reverse_test.go Normal file
View File

@ -0,0 +1,37 @@
package src
import (
"fmt"
"testing"
)
var root *ListNode
var node2 *ListNode
func TestMain(t *testing.M) {
node5 := &ListNode{Val: 5, Next: nil}
node4 := &ListNode{Val: 4, Next: node5}
node3 := &ListNode{Val: 3, Next: node4}
node2 = &ListNode{Val: 2, Next: node3}
node1 := &ListNode{Val: 1, Next: node2}
root = &ListNode{Val: 0, Next: node1}
fmt.Printf("%v\n", root)
t.Run()
}
func TestReverse(t *testing.T) {
fmt.Printf("%v\n", reverse(root))
}
func TestReverseRec(t *testing.T) {
fmt.Printf("%v\n", reverseRec(root))
}
func TestReverseKGroup(t *testing.T) {
fmt.Printf("%v\n", reverseKGroup(root, 2))
}
func TestReverseN(t *testing.T) {
fmt.Printf("%v\n", reverseN(root, node2))
}