Skip to content

Commit e49b8d4

Browse files
committed
feat: finish 38 83 problem
1 parent db1377d commit e49b8d4

File tree

6 files changed

+1012
-48
lines changed

6 files changed

+1012
-48
lines changed

algorithm/38.CountandSay/main.go

+13-24
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,28 @@
11
package countAndSay
22

33
import (
4-
"fmt"
54
"strconv"
65
)
76

87
func countAndSay(n int) string {
98
res := "1"
109
for n > 1 {
11-
fmt.Println("=============times ============", n, res)
1210
n--
1311
temp := ""
14-
for len(res) > 0 {
15-
times := 0
16-
for len(res) > 0 && res[0] == '1' {
17-
times++
18-
res = res[1:]
19-
}
20-
if times == 1 {
21-
if len(res) == 0 {
22-
temp += "11"
23-
continue
24-
} else {
25-
temp += "1"
26-
}
27-
} else if times == 0 {
28-
temp += "1"
29-
temp += res[0:1]
30-
} else if times > 1 {
31-
temp += strconv.Itoa(times)
32-
temp += "1"
33-
}
34-
if len(res) > 0 {
35-
res = res[1:]
12+
j := 0
13+
for j < len(res) {
14+
15+
count := 1
16+
e := res[j]
17+
18+
for j < len(res)-1 && res[j] == res[j+1] {
19+
count++
20+
j++
3621
}
22+
j++
23+
temp += strconv.Itoa(count)
24+
temp += string(e)
25+
3726
}
3827
res = temp
3928
}

algorithm/38.CountandSay/main_test.go

+4-24
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,10 @@ var tcs = []struct {
1010
N1 int
1111
ans string
1212
}{
13-
// {
14-
// 1,
15-
// "1",
16-
// },
17-
// {
18-
// 2,
19-
// "11",
20-
// },
21-
// {
22-
// 3,
23-
// "21",
24-
// },
25-
// {
26-
// 4,
27-
// "1211",
28-
// },
29-
// {
30-
// 5,
31-
// "111221",
32-
// },
33-
// {
34-
// 6,
35-
// "312211",
36-
// },
13+
{
14+
6,
15+
"312211",
16+
},
3717
}
3818

3919
func Test_bitwiseComplement(t *testing.T) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# [83. Remove Duplicates from Sorted List](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)
2+
3+
## 2019/09/15
4+
5+
### 题目 💗[medium]
6+
7+
Given a sorted linked list, delete all duplicates such that each element appear only once.
8+
9+
Example 1:
10+
11+
```bash
12+
Input: 1->1->2
13+
Output: 1->2
14+
```
15+
16+
Example 2:
17+
18+
```bash
19+
Input: 1->1->2->3->3
20+
Output: 1->2->3
21+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package deleteDuplicates
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
type ListNode = Helper.ListNode
8+
9+
func deleteDuplicates(head *ListNode) *ListNode {
10+
arr := []int{}
11+
for head != nil {
12+
// remove duplicate
13+
if lastIndexOf(arr, head.Val) == -1 {
14+
arr = append(arr, head.Val)
15+
}
16+
head = head.Next
17+
}
18+
node := &ListNode{Val: 0}
19+
newHead := node
20+
for len(arr) > 0 {
21+
node.Next = &ListNode{Val: arr[0]}
22+
node = node.Next
23+
arr = arr[1:]
24+
}
25+
return newHead.Next
26+
}
27+
28+
func lastIndexOf(nums []int, val int) int {
29+
for i := len(nums) - 1; i >= 0; i-- {
30+
if nums[i] == val {
31+
return i
32+
}
33+
}
34+
return -1
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package deleteDuplicates
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *ListNode
13+
ans *ListNode
14+
}{
15+
{
16+
Helper.Ints2LinkList([]int{1, 1, 2}),
17+
Helper.Ints2LinkList([]int{1, 2}),
18+
},
19+
{
20+
Helper.Ints2LinkList([]int{1, 1, 2, 3, 3}),
21+
Helper.Ints2LinkList([]int{1, 2, 3}),
22+
},
23+
}
24+
25+
func Test_bitwiseComplement(t *testing.T) {
26+
ast := assert.New(t)
27+
for _, tc := range tcs {
28+
ast.Equal(tc.ans, deleteDuplicates(tc.N1), "输入:%v", tc)
29+
}
30+
}

0 commit comments

Comments
 (0)