Skip to content

Commit c137a06

Browse files
author
limingzhong
committed
practice
1 parent 5e524d2 commit c137a06

File tree

5 files changed

+162
-0
lines changed

5 files changed

+162
-0
lines changed

TODO.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66
- [ ] 工程实现用到的算法解析
77
- [ ] 周赛计划
88
- [ ] 面试体系计划
9+
10+
https://www.ctolib.com/0xAX-go-algorithms.html

src/linkedlist/list.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package linkedlist
2+
3+
import "fmt"
4+
5+
type ListNode struct {
6+
val interface{}
7+
pre *ListNode
8+
next *ListNode
9+
}
10+
11+
func NewIntList(nums []int) *ListNode {
12+
if len(nums) == 0 {
13+
return nil
14+
}
15+
dummy := &ListNode{}
16+
head := dummy
17+
for _, val := range nums {
18+
node := &ListNode{
19+
val: val,
20+
}
21+
node.pre = head
22+
head.next = node
23+
head = node
24+
}
25+
26+
return dummy.next
27+
}
28+
29+
func (head *ListNode) Print() {
30+
curr := head
31+
for curr != nil {
32+
fmt.Printf("%v, ", curr.val)
33+
curr = curr.next
34+
}
35+
println()
36+
}
37+
38+
func (head *ListNode) String() string {
39+
s := ""
40+
curr := head
41+
for curr != nil {
42+
s += fmt.Sprintf("%v, ", curr.val)
43+
curr = curr.next
44+
}
45+
return s
46+
}
47+
48+
// 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
49+
func DeleteDuplicates(head *ListNode) *ListNode {
50+
current := head
51+
for current != nil {
52+
for current.next != nil && current.next.val == current.val {
53+
current.next = current.next.next
54+
}
55+
current = current.next
56+
}
57+
58+
return head
59+
}
60+
61+
// 给定一个排序链表,删除所有含有重复数字的节点,只保留原始链表中   没有重复出现的数字。
62+
// 只出现一次的节点
63+
func DeleteDupKeepOnce(head *ListNode) *ListNode {
64+
dummy := &ListNode{}
65+
dummy.next = head
66+
current := dummy
67+
68+
var rmVal interface{}
69+
for current.next != nil && current.next.next != nil {
70+
if current.next.val == current.next.next.val {
71+
rmVal = current.next.val
72+
// 指针直接跳到第三个节点
73+
current.next = current.next.next.next
74+
// 顺序删除
75+
for current.next != nil && current.next.val == rmVal {
76+
current.next = current.next.next
77+
}
78+
} else {
79+
current = current.next
80+
}
81+
}
82+
83+
return dummy.next
84+
}
85+
86+
// 反转一个单链表。
87+
// 思路:用一个 prev 节点保存向前指针,temp 保存向后的临时指针
88+
func ReverseList(head *ListNode) *ListNode {
89+
var pre *ListNode
90+
for head != nil {
91+
temp := head.next
92+
head.next = pre
93+
pre = head
94+
head = temp
95+
}
96+
97+
return pre
98+
}

src/linkedlist/list_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package linkedlist
2+
3+
import "testing"
4+
5+
func defaultSortedList() *ListNode {
6+
return NewIntList([]int{1, 2, 2, 2, 2, 3, 3, 4, 5, 5})
7+
}
8+
9+
func TestNewIntList(t *testing.T) {
10+
list := defaultSortedList()
11+
list.Print()
12+
println(list.String())
13+
}
14+
15+
func TestDeleteDuplicates(t *testing.T) {
16+
head := defaultSortedList()
17+
head.Print()
18+
DeleteDuplicates(head)
19+
head.Print()
20+
}
21+
22+
func TestDeleteDupKeepOnce(t *testing.T) {
23+
head := defaultSortedList()
24+
head.Print()
25+
DeleteDupKeepOnce(head)
26+
head.Print()
27+
}
28+
29+
func TestReverseList(t *testing.T) {
30+
head := defaultSortedList()
31+
head.Print()
32+
ReverseList(head).Print()
33+
}

src/str/str.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package str
2+
3+
// O(m*n)
4+
func Contain(haystack string, needle string) int {
5+
if len(needle) == 0 {
6+
return 0
7+
}
8+
var i, j int
9+
for i = 0; i < len(haystack)-len(needle)+1; i++ {
10+
for ; j < len(needle); j++ {
11+
if haystack[i+j] != needle[j] {
12+
break
13+
}
14+
}
15+
if j == len(needle) {
16+
return i
17+
}
18+
}
19+
return -1
20+
}

src/str/str_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package str
2+
3+
import "testing"
4+
5+
func TestContain(t *testing.T) {
6+
str := "abcdeqqqqqwww"
7+
needle := "www"
8+
println(Contain(str, needle))
9+
}

0 commit comments

Comments
 (0)