Skip to content

Commit 895cc27

Browse files
committed
Add offer100
1 parent 798d97f commit 895cc27

File tree

6 files changed

+139
-0
lines changed

6 files changed

+139
-0
lines changed

algorithms/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@
6868
|1108|[地址无效化](https://leetcode-cn.com/problems/defanging-an-ip-address/)|[go](./mystring/1108.defangIPaddr.go)|S|
6969
|1213|[三个有序数组的交集](https://leetcode-cn.com/problems/intersection-of-three-sorted-arrays/)|[go](./myarray/1213.arraysIntersection.go)|S|
7070
|1367|[二叉树中的列表](https://leetcode-cn.com/problems/linked-list-in-binary-tree/)|[go](./tree/1367.isSubPath.go)|M|
71+
|-|-|-|-|
72+
|58|[左旋转字符串](https://leetcode-cn.com/problems/zuo-xuan-zhuan-zi-fu-chuan-lcof/)|[go](./offer100/58.reverseLeftWords.go)|S|
73+
|22|[链表中倒数第k个节点](https://leetcode-cn.com/problems/lian-biao-zhong-dao-shu-di-kge-jie-dian-lcof/)|[go](./offer100/22.getKthFromEnd.go)|S
7174
||||[:top:](#Top)|
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package offer100
2+
3+
import "fmt"
4+
5+
// 由于链表只能从Head开始遍历,如果要计算链表长度则需要遍历一次,
6+
// 为减少时间复杂度,可以使用快慢指针法
7+
func getKthFromEnd(head *ListNode, k int) *ListNode {
8+
if head == nil {
9+
return nil
10+
}
11+
12+
slow, fast := head, head
13+
14+
// 快指针先移动k步,fast != nil是为了处理当 n > len(链表) 越界情况
15+
for k != 0 && fast != nil {
16+
fast = fast.Next
17+
k--
18+
}
19+
20+
// if n >= len(链表),返回头结点
21+
if fast == nil {
22+
return head
23+
}
24+
fmt.Println(fast.Val)
25+
26+
// 快慢指针同时开始移动
27+
for fast != nil {
28+
fast = fast.Next
29+
slow = slow.Next
30+
}
31+
32+
return slow
33+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package offer100
2+
3+
import (
4+
"Leetcode/algorithms/kit"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
// run: go test -run Test_getKthFromEnd
10+
func Test_getKthFromEnd(t *testing.T) {
11+
cases := []CaseEntry{
12+
{
13+
Name: "x1",
14+
Input: []interface{}{
15+
[]int{1, 2, 3, 4, 5}, 2,
16+
},
17+
Expected: []int{4, 5},
18+
},
19+
{
20+
Name: "x1",
21+
Input: []interface{}{
22+
[]int{1, 2, 3, 4, 5}, 4,
23+
},
24+
Expected: []int{1, 2, 3, 4, 5},
25+
},
26+
}
27+
28+
for _, c := range cases {
29+
t.Run(c.Name, func(t *testing.T) {
30+
head := kit.Ints2List(c.Input.([]interface{})[0].([]int))
31+
k := c.Input.([]interface{})[1].(int)
32+
33+
out2Ints := kit.List2Ints(getKthFromEnd(head, k))
34+
exp2Ints := c.Expected.([]int)
35+
if !reflect.DeepEqual(out2Ints, exp2Ints) {
36+
t.Errorf("getKthFromEnd(%v, %d)=%v, expected=%v", c.Input.([]interface{})[0].([]int), k, out2Ints, exp2Ints)
37+
}
38+
})
39+
}
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package offer100
2+
3+
// 利用字符串切片
4+
func reverseLeftWords1(s string, n int) string {
5+
return s[n:] + s[:n]
6+
}
7+
8+
// 遍历,巧妙利用mod
9+
func reverseLeftWords2(s string, n int) string {
10+
var ans string
11+
for i := n; i < len(s)+n; i++ {
12+
ans += string(s[i%len(s)])
13+
}
14+
return ans
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package offer100
2+
3+
import (
4+
"testing"
5+
)
6+
7+
// run: go test -run Test_reverseLeftWords
8+
func Test_reverseLeftWords(t *testing.T) {
9+
cases := []CaseEntry{
10+
{
11+
Name: "x1",
12+
Input: []interface{}{
13+
"abcdefg", 2,
14+
},
15+
Expected: "cdefgab",
16+
},
17+
{
18+
Name: "x2",
19+
Input: []interface{}{
20+
"lrloseumgh", 6,
21+
},
22+
Expected: "umghlrlose",
23+
},
24+
}
25+
26+
for _, c := range cases {
27+
t.Run(c.Name, func(t *testing.T) {
28+
s := c.Input.([]interface{})[0].(string)
29+
k := c.Input.([]interface{})[1].(int)
30+
if output := reverseLeftWords1(s, k); output != c.Expected.(string) {
31+
t.Errorf("reverseLeftWords1(%s, %d)=%s, expected=%s", s, k, output, c.Expected.(string))
32+
}
33+
34+
if output := reverseLeftWords2(s, k); output != c.Expected.(string) {
35+
t.Errorf("reverseLeftWords2(%s, %d)=%s, expected=%s", s, k, output, c.Expected.(string))
36+
}
37+
})
38+
}
39+
}

algorithms/offer100/base.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package offer100
2+
3+
import (
4+
"Leetcode/algorithms/kit"
5+
)
6+
7+
type CaseEntry = kit.CaseEntry
8+
type ListNode = kit.ListNode
9+
type DListNode = kit.DListNode

0 commit comments

Comments
 (0)