Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
nixijnat committed Feb 21, 2021
1 parent ead8c8e commit aa55511
Show file tree
Hide file tree
Showing 10 changed files with 286 additions and 4 deletions.
3 changes: 2 additions & 1 deletion 0001-0020/0003/0003_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func TestFunc(t *testing.T) {
{"tmmzuxt", 5},
{"cdd", 2},
} {
t.Log(c.input, lengthOfLongestSubstring(c.input), c.expect)
v := lengthOfLongestSubstring(c.input)
t.Log(c, v, v == c.expect)
}
}
9 changes: 7 additions & 2 deletions 0001-0020/0020/0020_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ func TestFunc(t *testing.T) {
input string
expect bool
}{
{"", false},
{"{[]}", true},
{"([)]", false},
{"(]", false},
{"()[]{}", true},
{"()", true},
} {
t.Log(c, isValid(c.input))
v := isValid(c.input)
t.Log(c, v, v == c.expect)
}
}
20 changes: 20 additions & 0 deletions 0021-0050/0031/0031_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"reflect"
"testing"
)

func TestFunc(t *testing.T) {
for _, c := range []struct {
input1 []int
expect []int
}{
{[]int{1, 2, 3}, []int{1, 3, 2}},
{[]int{3, 2, 1}, []int{1, 2, 3}},
{[]int{1, 1, 5}, []int{1, 5, 1}},
} {
nextPermutation(c.input1)
t.Log(c, reflect.DeepEqual(c.expect, c.input1))
}
}
3 changes: 2 additions & 1 deletion 0050-0100/0056/0056_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"reflect"
"testing"
)

Expand All @@ -15,6 +16,6 @@ func TestFunc(t *testing.T) {
{[][]int{[]int{1, 5}, []int{2, 3}, []int{8, 10}, []int{4, 6}}, [][]int{[]int{1, 6}, []int{8, 10}}},
} {
v := merge(c.input1)
t.Log(c, v)
t.Log(c, v, reflect.DeepEqual(v, c.expect))
}
}
53 changes: 53 additions & 0 deletions 0050-0100/0076/0076.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,56 @@ func minWindow(s, t string) string {
}
return ret
}
func minWindow_new(s, t string) string {
if s == "" || len(s) < len(t) {
return ""
}
// 统计字符频次
var freq [128]int
for i := range t {
freq[t[i]]++
}
// 记录未匹配的字符数
var unmatchCnt = len(t)
var count [128]int
var res string
for l, r := 0, 0; r < len(s); r++ {
// 右指针扩张寻找目标字串
val := s[r]
// 忽略无关字符
if freq[val] == 0 {
continue
}
// 更新未匹配次数
if count[val] < freq[val] {
unmatchCnt--
}
count[val]++ // 记录实际字符频次
if unmatchCnt != 0 {
continue
}
// 左指针收缩,直到不满足要求
// 注意:佐治
for ; ; l++ {
// 更新结果
if res == "" || len(res) > len(s[l:r+1]) {
res = s[l : r+1]
}
val := s[l]
// 忽略无关字符
if freq[val] == 0 {
continue
}
count[val]--
// 频次不满足要求,则退出
if count[val] < freq[val] {
unmatchCnt++
// 注意,退出前将左指针指向 相关字符
for l++; l < r && freq[s[l]] == 0; l++ {
}
break
}
}
}
return res
}
82 changes: 82 additions & 0 deletions 0101-0200/0145/0145.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func postorderTraversal1(root *TreeNode) []int {
res := []int{}
helper(root, &res)
return res
}
func helper(root *TreeNode, ns *[]int) {
if root == nil {
return
}
helper(root.Left, ns)
helper(root.Right, ns)
*ns = append((*ns), root.Val)
}

// 迭代:visit数组
func postorderTraversal_visit(root *TreeNode) []int {
if root == nil {
return nil
}
res := []int{}
stk := make([]*TreeNode, 0, 8)
stk = append(stk, root)
visit := make([]bool, 1, 8) // 标识是否已经释放子节点
for len(stk) > 0 {
n := len(stk)
root = stk[n-1]
if visit[n-1] { //
res = append(res, root.Val)
stk = stk[:n-1]
visit = visit[:n-1]
continue
}
// 添加子节点
visit[n-1] = true
if root.Right != nil {
stk = append(stk, root.Right)
visit = append(visit, false)
}
if root.Left != nil {
stk = append(stk, root.Left)
visit = append(visit, false)
}
}
return res
}

// 迭代:无 visit数组
func postorderTraversal(root *TreeNode) []int {
if root == nil {
return nil
}
res := []int{}
stk := make([]*TreeNode, 0, 8)
var prev *TreeNode // 记录前一个访问的节点
for root != nil || len(stk) > 0 {
// 左儿子入栈
for ; root != nil; root = root.Left {
stk = append(stk, root)
}
n := len(stk)
root = stk[n-1]
// 没有右儿子则访问
// 右儿子和前一个访问节点一样则访问:回溯
if root.Right == nil || root.Right == prev {
res = append(res, root.Val)
stk = stk[:n-1]
prev = root
root = nil // 置零
} else {
root = root.Right
}
}
return res
}
59 changes: 59 additions & 0 deletions 0101-0200/0145/0145_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"container/list"
"reflect"
"testing"
)

func makeTree(arr []int) *TreeNode {
l := len(arr)
if l == 0 {
return nil
}
var mk func(i int) *TreeNode
mk = func(i int) *TreeNode {
if i >= l || arr[i] == 0 {
return nil
}
root := &TreeNode{arr[i], nil, nil}
root.Left = mk(2*i + 1)
root.Right = mk(2*i + 2)
return root
}
return mk(0)
}

func resovleTree(t *TreeNode) [][]int {
l := list.New()
l.PushBack(t)
res := make([][]int, 0, 4)
for l.Len() != 0 {
v := l.Remove(l.Front()).(*TreeNode)
tmp := []int{v.Val, -1, -1}
if v.Left != nil {
l.PushBack(v.Left)
tmp[1] = v.Left.Val
}
if v.Right != nil {
l.PushBack(v.Right)
tmp[2] = v.Right.Val
}
res = append(res, tmp)
}
return res
}

func TestFunc(t *testing.T) {
for _, c := range []struct {
input1 []int
expect []int
}{
{[]int{1, 2, 5, 3, 4, 0, 6}, []int{3, 4, 2, 6, 5, 1}},
{[]int{1, 0, 2, 0, 0, 3}, []int{3, 2, 1}},
} {
tree := makeTree(c.input1)
v := postorderTraversal(tree)
t.Log(c, v, reflect.DeepEqual(c.expect, v))
}
}
29 changes: 29 additions & 0 deletions 0301-0400/0394/0394.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,32 @@ func decodeString(s string) string {
}
return cur.String()
}

// 优秀的方法
func decodeString_good(s string) string {
stk := make([]byte, 0, 8)
for i := range s {
// 将 非 ] 全部放入 栈
if s[i] != ']' {
stk = append(stk, s[i])
continue
}
// 遇到 ] 后,向前找 [ 以获取重复的部分
leftIndex := bytes.LastIndex(stk, []byte{'['})
repeatBytes := stk[leftIndex+1:]
// 继续往 [ 前面找重复次数
numIndex := leftIndex - 1
for ; numIndex >= 0; numIndex-- {
if stk[numIndex] < '0' || stk[numIndex] > '9' {
break
}
}
numIndex++
// 将栈先截断
stk = stk[:numIndex]
count, _ := strconv.Atoi(string(stk[numIndex:leftIndex]))
// 再将重复的部分放入栈
stk = append(stk, bytes.Repeat(repeatBytes, count)...)
}
return string(stk)
}
31 changes: 31 additions & 0 deletions 0401-0500/0438/0438.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,34 @@ func findAnagrams(s, p string) []int {
}
return res
}

// 一般想法:将窗口维持再 p 大小
func findAnagrams_normal2(s, p string) []int {
if s == "" || len(s) < len(p) {
return nil
}
// 初始化目标频次和当前频次
// 将窗口维持在目标窗口大小
var freq [26]int
var counts [26]int
for i := range p {
freq[p[i]-'a']++
counts[s[i]-'a']++
}
res := make([]int, 0, 8)
if freq == counts {
res = append(res, 0)
}
// 滑动窗口
l, r := 0, len(p)
for r < len(s) {
counts[s[r]-'a']++
counts[s[l]-'a']--
r++
l++
if freq == counts {
res = append(res, l)
}
}
return res
}
1 change: 1 addition & 0 deletions 0501-0600/0581/0581_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func TestFunc(t *testing.T) {
{[]int{1, 2, 4, 6, 4, 5, 3, 5, 7, 8}, 6},
{[]int{1, 2, 3, 4}, 0},
{[]int{2, 6, 4, 8, 10, 9, 15}, 5},
{[]int{2, 4, 1, 5, 3}, 5},
{[]int{1, 4, 4, 4, 2, 3, 1, 4, 4, 3, 2, 4}, 10},
{[]int{1, 4, 4, 4, 2, 3, 1, 5, 7, 6, 8, 8}, 9},
} {
Expand Down

0 comments on commit aa55511

Please sign in to comment.