Skip to content

Commit 15c34f0

Browse files
author
beyondyyh
committed
add some
1 parent 7f68cf4 commit 15c34f0

File tree

6 files changed

+124
-3
lines changed

6 files changed

+124
-3
lines changed

algorithms/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
|1108|[地址无效化](https://leetcode-cn.com/problems/defanging-an-ip-address/)|[go](./mystring/1108.defangIPaddr.go)|S|
7070
|1213|[三个有序数组的交集](https://leetcode-cn.com/problems/intersection-of-three-sorted-arrays/)|[go](./myarray/1213.arraysIntersection.go)|S|
7171
|1367|[二叉树中的列表](https://leetcode-cn.com/problems/linked-list-in-binary-tree/)|[go](./tree/1367.isSubPath.go)|M|
72-
|-|-|-|-|
72+
|offer100||||
73+
|22|[链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)|[go](./offer100/22.getKthFromEnd.go)|S|
74+
|40|[最小的k个数](https://leetcode-cn.com/problems/zui-xiao-de-kge-shu-lcof/)|[go](./offer100/40.getLeastNumbers.go)|S|
7375
|58|[左旋转字符串](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)|[go](./offer100/58.reverseLeftWords.go)|S|
74-
|22|[链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)|[go](./offer100/22.getKthFromEnd.go)|S
7576
||||[:top:](#Top)|

algorithms/mystring/20.isValid.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,30 @@ func isValid2(s string) bool {
7575

7676
return stack.IsEmpty()
7777
}
78+
79+
// 遍历字符串,当遇到左括号时,先把右括号入栈,
80+
// 当遇到右括号时,如果此时栈为空则为无效,不为空则出栈一个元素(该元素应该是右括号才算匹配)与当前遍历到的元素对比是否相等,不相等说明肯定不匹配
81+
// 遍历结束判断栈是否为空
82+
func isValid3(s string) bool {
83+
// 奇数长度,肯定不匹配
84+
if len(s)%2 == 1 {
85+
return false
86+
}
87+
88+
stack := kit.NewStack()
89+
for _, char := range s {
90+
switch char {
91+
case '(':
92+
stack.Push(')')
93+
case '[':
94+
stack.Push(']')
95+
case '{':
96+
stack.Push('}')
97+
default: // 说明有多余的括号
98+
if stack.IsEmpty() || char != (stack.Pop()).(rune) {
99+
return false
100+
}
101+
}
102+
}
103+
return stack.IsEmpty()
104+
}

algorithms/mystring/20.isValid_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ func Test_isValid(t *testing.T) {
2525
if output := isValid2(tt.input); output != tt.expected {
2626
t.Errorf("isValid2(%s)=%t, expected=%t", tt.input, output, tt.expected)
2727
}
28+
if output := isValid3(tt.input); output != tt.expected {
29+
t.Errorf("isValid3(%s)=%t, expected=%t", tt.input, output, tt.expected)
30+
}
2831
})
2932
}
3033
}

algorithms/offer100/22.getKthFromEnd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package offer100
22

33
import (
4-
"Leetcode/algorithms/kit"
54
"reflect"
65
"testing"
6+
7+
"Leetcode/algorithms/kit"
78
)
89

910
// run: go test -run Test_getKthFromEnd
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package offer100
2+
3+
// 输入整数数组 arr ,找出其中最小的 k 个数。例如,输入4、5、1、6、2、7、3、8这8个数字,则最小的4个数字是1、2、3、4。
4+
// 示例 1:
5+
// 输入:arr = [3,2,1], k = 2
6+
// 输出:[1,2] 或者 [2,1]
7+
// 示例 2:
8+
// 输入:arr = [0,1,2,1], k = 1
9+
// 输出:[0]
10+
11+
import (
12+
"container/heap"
13+
)
14+
15+
// int类型的堆
16+
type IntHeap []int
17+
18+
// 利用golang标准库的container/heap实现堆
19+
func (h IntHeap) Len() int { return len(h) }
20+
func (h IntHeap) Less(i, j int) bool { return h[i] > h[j] } // '<'是小顶堆
21+
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
22+
23+
// Push和Pop 的receiver用指针,因为不仅修改slice的长度,还要修改其元素
24+
func (h *IntHeap) Push(x interface{}) {
25+
*h = append(*h, x.(int))
26+
}
27+
28+
func (h *IntHeap) Pop() interface{} {
29+
old := *h
30+
n := len(old)
31+
x := old[n-1]
32+
*h = old[0 : n-1]
33+
return x
34+
}
35+
36+
func getLeastNumbers(nums []int, k int) []int {
37+
h := &IntHeap{}
38+
heap.Init(h)
39+
for _, num := range nums {
40+
heap.Push(h, num)
41+
if h.Len() > k {
42+
heap.Pop(h)
43+
}
44+
}
45+
46+
res := make([]int, 0, k)
47+
for h.Len() > 0 {
48+
res = append(res, heap.Pop(h).(int))
49+
}
50+
return res
51+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package offer100
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
// run: go test -run -v Test_getLeastNumbers
9+
func Test_getLeastNumbers(t *testing.T) {
10+
cases := []CaseEntry{
11+
{
12+
Name: "x1",
13+
Input: []interface{}{
14+
[]int{3, 2, 1}, 2,
15+
},
16+
Expected: []int{2, 1},
17+
},
18+
{
19+
Name: "x2",
20+
Input: []interface{}{
21+
[]int{0, 1, 2, 1}, 1,
22+
},
23+
Expected: []int{0},
24+
},
25+
}
26+
27+
for _, c := range cases {
28+
t.Run(c.Name, func(t *testing.T) {
29+
nums := c.Input.([]interface{})[0].([]int)
30+
k := c.Input.([]interface{})[1].(int)
31+
32+
output := getLeastNumbers(nums, k)
33+
if !reflect.DeepEqual(output, c.Expected.([]int)) {
34+
t.Errorf("getLeastNumbers(%v, %d)=%v, expected=%v", nums, k, output, c.Expected)
35+
}
36+
})
37+
}
38+
}

0 commit comments

Comments
 (0)