Skip to content

Commit d6aaf42

Browse files
committed
add leetcode 82
1 parent cdecae5 commit d6aaf42

File tree

4 files changed

+122
-24
lines changed

4 files changed

+122
-24
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
type ListNode struct {
9+
Val int
10+
Next *ListNode
11+
}
12+
13+
// 比较结果
14+
func isEqual(l1 *ListNode, l2 *ListNode) bool {
15+
16+
for l1 != nil && l2 != nil {
17+
if l1.Val != l2.Val {
18+
return false
19+
}
20+
l1 = l1.Next
21+
l2 = l2.Next
22+
}
23+
if l1 == nil && l2 != nil {
24+
return false
25+
}
26+
if l1 != nil && l2 == nil {
27+
return false
28+
}
29+
return true
30+
}
31+
32+
func PrintList(head *ListNode) {
33+
for head != nil {
34+
fmt.Print(head.Val, "->")
35+
head = head.Next
36+
}
37+
fmt.Println()
38+
}
39+
40+
// 根据数组反序列化链表
41+
func UnmarshalListBySlice(nums []int) *ListNode {
42+
head := &ListNode{Val: -1, Next: nil}
43+
tmp := head
44+
for _, v := range nums {
45+
tmp.Next = &ListNode{Val: v, Next: nil}
46+
tmp = tmp.Next
47+
}
48+
return head.Next
49+
}
50+
51+
// 随机初始化链表
52+
func UnmarshalListByRand(max_num int, len int) *ListNode {
53+
head := &ListNode{Val: -1, Next: nil}
54+
tmp := head
55+
56+
for i := 0; i < len; i++ {
57+
tmp.Next = &ListNode{Val: rand.Intn(max_num), Next: nil}
58+
tmp = tmp.Next
59+
}
60+
return head.Next
61+
}
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,33 @@
1-
# [1. Add Sum][title]
1+
# [82. Remove Duplicates from Sorted List II[title]
22

33
## Description
44

5-
Given two binary strings, return their sum (also a binary string).
6-
7-
The input strings are both **non-empty** and contains only characters `1` or `0`.
5+
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
86

97
**Example 1:**
108

119
```
12-
Input: a = "11", b = "1"
13-
Output: "100"
10+
Input: 1->2->3->3->4->4->5
11+
Output: 1->2->5
1412
```
1513

1614
**Example 2:**
1715

1816
```
19-
Input: a = "1010", b = "1011"
20-
Output: "10101"
17+
Input: 1->1->1->2->3
18+
Output: 2->3
2119
```
2220

23-
**Tags:** Math, String
21+
**Tags:** Linked List
2422

2523
## 题意
26-
>给你两个二进制串,求其和的二进制串
24+
> 给定一个有序的单链表,删除其中的重复元素
2725
2826
## 题解
2927

3028
### 思路1
31-
> 按照小学算数那么来做,用 `carry` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
29+
> 使用两个指针,一个指针指向当前元素,另一个指针指向前一个元素。
30+
> 不断判断当前元素和下一个元素是否相等,如果相等,当前元素指向下一个,否则,令前一个指针指向当前节点的下一个节点。
3231
3332
```go
3433

@@ -44,5 +43,5 @@ Output: "10101"
4443

4544
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
4645

47-
[title]: https://leetcode.com/problems/two-sum/description/
46+
[title]: https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii/description/
4847
[me]: https://github.com/kylesliu/awesome-golang-leetcode
Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,36 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
/*
4+
* @lc app=leetcode id=82 lang=golang
5+
*
6+
* [82] Remove Duplicates from Sorted List II
7+
*/
8+
/**
9+
* Definition for singly-linked list.
10+
* type ListNode struct {
11+
* Val int
12+
* Next *ListNode
13+
* }
14+
*/
15+
func Solution(head *ListNode) *ListNode {
16+
if nil == head || nil == head.Next {
17+
return head
18+
}
19+
p := new(ListNode)
20+
p.Next = head
21+
prev, current := p, p.Next
22+
for current != nil && current.Next != nil {
23+
if current.Val == current.Next.Val {
24+
for current.Next != nil && current.Val == current.Next.Val {
25+
current = current.Next
26+
}
27+
current = current.Next
28+
prev.Next = current
29+
} else {
30+
prev = prev.Next
31+
current = current.Next
32+
}
33+
}
34+
return p.Next
535
}
36+

src/0082.Remove-Duplicates-from-Sorted-List-II/Solution_test.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
package Solution
22

33
import (
4-
"reflect"
54
"testing"
65
)
76

87
func TestSolution(t *testing.T) {
98
// 测试用例
109
cases := []struct {
1110
name string
12-
inputs bool
13-
expect bool
11+
inputs []int
12+
expect []int
1413
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
14+
{
15+
"TestCase 1",
16+
[]int{1,2,3,3,4,4,5},
17+
[]int{1,2,5},
18+
},
19+
{
20+
"TestCase 2",
21+
[]int{1,1,1,2,3},
22+
[]int{2,3},
23+
},
1824
}
1925

2026
// 开始测试
2127
for _, c := range cases {
2228
t.Run(c.name, func(t *testing.T) {
23-
ret := Solution(c.inputs)
24-
if !reflect.DeepEqual(ret, c.expect) {
25-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
29+
ret := Solution(UnmarshalListBySlice(c.inputs))
30+
if !isEqual(ret, UnmarshalListBySlice(c.expect)) {
31+
PrintList(ret)
32+
PrintList(UnmarshalListBySlice(c.expect))
33+
t.Fatalf("expected: %v, but got: %v, with inputs: %v", c.expect, ret, c.inputs)
2734
}
2835
})
2936
}

0 commit comments

Comments
 (0)