-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_test.go
88 lines (78 loc) · 1.61 KB
/
test_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package coding_practice
import (
"fmt"
"math"
"testing"
)
func TestName(t *testing.T) {
fmt.Printf("%.0f", math.MaxFloat32)
}
func TestSpace(t *testing.T) {
c := ' '
k := "blue"
r := k + string(c) + k
fmt.Println(r)
}
func reverseWords(s string) string {
temp := ""
res := ""
for i := len(s) - 1; i >= 0; i-- {
if s[i] == ' ' {
if temp != "" {
if res == "" {
res += temp
} else {
res += " " + temp
}
temp = ""
}
continue
}
temp = string(s[i]) + temp
}
if temp != "" {
if res != "" {
res += " "
}
res += temp
}
return res
}
func maxSlidingWindow(nums []int, k int) []int {
// 申明一个切片,当作队列使用
// 要维护的是一个单调递减的队列, 注意:q中放的值是nums中的下标,
//而不是具体的值,这样才能保最大值是否是在k位之中
q := []int{}
// 定义push方法
push := func(i int) {
// 维护的是单调递减序列,队首是最大值, 如过 要加入的大于屁股上的,就弹出屁股那的
for len(q) > 0 && nums[i] >= nums[q[len(q)-1]] {
q = q[:len(q)-1]
}
// 这是往屁股后边加入
q = append(q, i)
}
//先加 k个进去,懂得都懂
for i := 0; i < k; i++ {
push(i)
}
n := len(nums)
//这个是答案
ans := make([]int, 1, n-k+1)
ans[0] = nums[q[0]]
for i := k; i < n; i++ {
// 入队
push(i)
for q[0] <= i-k {
// 判断队首是不是 不合法(不和法值得是是不是在这k位之中)
q = q[1:]
}
ans = append(ans, nums[q[0]])
}
return ans
}
func TestSlice(t *testing.T) {
q := []int{1, 2, 3, 4, 5}
q = q[:len(q)-1]
fmt.Println(q)
}