File tree 17 files changed +410
-43
lines changed
0021.merge-two-sorted-lists
0026.remove-duplicates-from-sorted-array
17 files changed +410
-43
lines changed Original file line number Diff line number Diff line change 2
2
✅ ** LeetCode** 题解
3
3
4
4
[ ![ Golang] ( https://img.shields.io/badge/language-Golang-blue.svg )] ( https://img.shields.io/badge/language-Golang-blue.svg )
5
- [ ![ Golang] ( https://img.shields.io/badge/language-Php-red.svg )] ( https://img.shields.io/badge/language-Php-red.svg )
6
- [ ![ Golang] ( https://img.shields.io/badge/language-Python-yellow.svg )] ( https://img.shields.io/badge/language-Python-yellow.svg )
7
- [ ![ MIT License] ( https://img.shields.io/badge/License-MIT-green.svg )] ( LICENSE )
8
5
[ ![ Travis] ( https://www.travis-ci.org/lxlxw/leetcode.svg?branch=master )] ( https://www.travis-ci.org/lxlxw/leetcode )
9
6
[ ![ Codecov] ( https://codecov.io/gh/lxlxw/leetcode/branch/master/graph/badge.svg )] ( https://codecov.io/gh/lxlxw/leetcode )
7
+ [ ![ MIT License] ( https://img.shields.io/badge/License-MIT-green.svg )] ( LICENSE )
10
8
[ ![ HitCount] ( http://hits.dwyl.com/lxlxw/leetcode.svg )] ( http://hits.dwyl.com/lxlxw/leetcode )
11
9
12
10
[ [ 中文] ( http://leetcode.xwlin.com )
13
11
| [ English] ( http://leetcode.xwlin.com/#/en/ )
14
12
]
15
13
16
- 😀 使用` Golang ` / ` Python ` / ` PHP ` 等语言实现LeetCode题解 (持续更新中...)
14
+ 😀 使用` Golang ` 语言实现LeetCode题解 (持续更新中...)
17
15
18
16
## 项目简介
19
17
@@ -24,7 +22,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
24
22
- 可根据题目难易度与题型标签进行` 快速索引 ` 。
25
23
- 包含leetcode问题链接的` 说明 ` 与` 示例 ` 。
26
24
- 包含leetcode` 解题思路 ` 和具体的` 代码实现 ` 。
27
- - 包含单元测试 ,已提交的问题均已通过leetcode各自的测试用例。
25
+ - 包含完整的单元测试 ,已提交的问题均已通过leetcode各自的测试用例。
28
26
- 可以直接通过编辑页面实现` Pull requests ` 。
29
27
30
28
Original file line number Diff line number Diff line change 13
13
- [ 9.回文数 ✅] ( solution/1-99/0009.palindrome-number/ )
14
14
- [ 13.罗马数字转整数 ✅] ( solution/1-99/0013.roman-to-integer/ )
15
15
- [ 20.有效的括号 ✅] ( solution/1-99/0020.valid-parentheses/ )
16
+ - [ 21.合并两个有序链表 ✅] ( solution/1-99/0021.merge-two-sorted-lists/ )
16
17
- [ 26.删除排序数组中的重复项 ✅] ( solution/1-99/0026.remove-duplicates-from-sorted-array/ )
18
+ - [ 27.移除元素 ✅] ( solution/1-99/0027.remove-element/ )
17
19
18
20
19
21
Original file line number Diff line number Diff line change 23
23
| --- | --- | --- | --- | --- |
24
24
| [ 1] ( https://leetcode-cn.com/problems/two-sum ) | [ 两数之和] ( /solution/1-99/0001.two-sum/ ) | ` 数组 ` ,` 哈希表 ` | <font color =green >简单</font > | ✅ |
25
25
| [ 26] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array ) | [ 删除排序数组中的重复项] ( /solution/1-99/0026.remove-duplicates-from-sorted-array/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
26
+ | [ 27] ( https://leetcode-cn.com/problems/remove-element ) | [ 移除元素] ( /solution/1-99/0027.remove-element/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
26
27
27
28
#### ** 哈希表**
28
29
38
39
39
40
#### ** 堆**
40
41
42
+ #### ** 链表**
43
+
44
+ | 题号 | 题解 | 标签 | 难度 | 是否解题 |
45
+ | --- | --- | --- | --- | --- |
46
+ | [ 21] ( https://leetcode-cn.com/problems/merge-two-sorted-lists ) | [ 合并两个有序链表] ( /solution/1-99/0021.merge-two-sorted-lists/ ) | ` 链表 ` | <font color =green >简单</font > | ✅ |
47
+
41
48
#### ** 排序**
42
49
43
50
#### ** 队列**
53
60
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
54
61
| --- | --- | --- | --- | --- |
55
62
| [ 26] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array ) | [ 删除排序数组中的重复项] ( /solution/1-99/0026.remove-duplicates-from-sorted-array/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
63
+ | [ 27] ( https://leetcode-cn.com/problems/remove-element ) | [ 移除元素] ( /solution/1-99/0027.remove-element/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
56
64
57
65
#### ** 递归**
58
66
Original file line number Diff line number Diff line change 13
13
| [ 13] ( https://leetcode-cn.com/problems/roman-to-integer ) | [ 罗马数字转整数] ( /solution/1-99/0013.roman-to-integer/ ) | ` 数学 ` ,` 字符串 ` | <font color =green >简单</font > | ✅ |
14
14
| [ 14] ( https://leetcode-cn.com/problems/longest-common-prefix ) | [ 最长公共前缀] ( /solution/1-99/0014.longest-common-prefix/ ) | ` 字符串 ` | <font color =green >简单</font > |
15
15
| [ 20] ( https://leetcode-cn.com/problems/valid-parentheses ) | [ 有效的括号] ( /solution/1-99/0020.valid-parentheses/ ) | ` 栈 ` ,` 字符串 ` | <font color =green >简单</font > | ✅ |
16
- | [ 21] ( https://leetcode-cn.com/problems/merge-two-sorted-lists ) | [ 合并两个有序链表] ( /solution/1-99/0021.merge-two-sorted-lists/ ) | ` 链表 ` | <font color =green >简单</font > |
16
+ | [ 21] ( https://leetcode-cn.com/problems/merge-two-sorted-lists ) | [ 合并两个有序链表] ( /solution/1-99/0021.merge-two-sorted-lists/ ) | ` 链表 ` | <font color =green >简单</font > | ✅ |
17
17
| [ 26] ( https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array ) | [ 删除排序数组中的重复项] ( /solution/1-99/0026.remove-duplicates-from-sorted-array/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
18
- | [ 27] ( https://leetcode-cn.com/problems/remove-element ) | [ 移除元素] ( /solution/1-99/0027.remove-element/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > |
18
+ | [ 27] ( https://leetcode-cn.com/problems/remove-element ) | [ 移除元素] ( /solution/1-99/0027.remove-element/ ) | ` 数组 ` ,` 双指针 ` | <font color =green >简单</font > | ✅ |
19
19
| [ 28] ( https://leetcode-cn.com/problems/implement-strstr ) | [ 实现 strstr()] ( /solution/1-99/0028.implement-strstr%28%29/ ) | ` 双指针 ` ,` 字符串 ` | <font color =green >简单</font > |
20
20
| [ 35] ( https://leetcode-cn.com/problems/search-insert-position ) | [ 搜索插入位置] ( /solution/1-99/0035.search-insert-position/ ) | ` 数组 ` ,` 二分查找 ` | <font color =green >简单</font > |
21
21
| [ 38] ( https://leetcode-cn.com/problems/count-and-say ) | [ 外观数列] ( /solution/1-99/0038.count-and-say/ ) | ` 字符串 ` | <font color =green >简单</font > |
Original file line number Diff line number Diff line change @@ -37,10 +37,6 @@ func twoSum(nums []int, target int) []int {
37
37
return []int {}
38
38
}
39
39
```
40
- #### ** Java**
41
40
42
- #### ** Python**
43
-
44
- #### ** PHP**
45
41
46
42
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -29,10 +29,5 @@ func sk() error {
29
29
return nil
30
30
}
31
31
```
32
- #### ** Java**
33
-
34
- #### ** Python**
35
-
36
- #### ** PHP**
37
32
38
33
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -53,10 +53,5 @@ func reverse(x int) int {
53
53
return ret
54
54
}
55
55
```
56
- #### ** Java**
57
-
58
- #### ** Python**
59
-
60
- #### ** PHP**
61
56
62
57
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -51,10 +51,4 @@ func isPalindrome(x int) bool {
51
51
return true
52
52
}
53
53
```
54
- #### ** Java**
55
-
56
- #### ** Python**
57
-
58
- #### ** PHP**
59
-
60
54
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -61,7 +61,7 @@ M 1000</pre>
61
61
1 . 先匹配特殊的字符串,匹配到的话` ret ` 累加
62
62
2 . 遍历字符串,如果能在` romanMap ` 中找到的话` ret ` 累加
63
63
64
- ?> 该题解不是最优解!
64
+ ?> 说明: 该题解不是最优解!
65
65
66
66
### 代码实现
67
67
@@ -107,10 +107,5 @@ func romanToInt(s string) int {
107
107
return ret
108
108
}
109
109
```
110
- #### ** Java**
111
-
112
- #### ** Python**
113
-
114
- #### ** PHP**
115
110
116
111
<!-- tabs:end -->
Original file line number Diff line number Diff line change @@ -84,10 +84,5 @@ func isValid(s string) bool {
84
84
return stack.Len () == 0
85
85
}
86
86
```
87
- #### ** Java**
88
-
89
- #### ** Python**
90
-
91
- #### ** PHP**
92
87
93
88
<!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ # [ 21.合并两个有序链表] ( https://leetcode-cn.com/problems/merge-two-sorted-lists )
2
+
3
+
4
+ ### 题目描述
5
+
6
+ <div class =" notranslate " ><p >将两个升序链表合并为一个新的 <strong >升序</strong > 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。  ; </p >
7
+
8
+ <p >  ; </p >
9
+
10
+ <p ><strong >示例:</strong ></p >
11
+
12
+ <pre ><strong >输入:</strong >1-> ; 2-> ; 4, 1-> ; 3-> ; 4
13
+ <strong >输出:</strong >1-> ; 1-> ; 2-> ; 3-> ; 4-> ; 4
14
+ </pre >
15
+ </div >
16
+
17
+ ### 解题思路
18
+ ![ ] ( http://lc-photo.xwlin.com/21-1.png )
19
+ ![ ] ( http://lc-photo.xwlin.com/21-2.png )
20
+ ![ ] ( http://lc-photo.xwlin.com/21-3.png )
21
+
22
+ ### 代码实现
23
+
24
+ <!-- tabs:start -->
25
+
26
+ #### ** Golang 1**
27
+ ``` go
28
+ func mergeTwoLists1 (l1 *ListNode , l2 *ListNode ) *ListNode {
29
+ if l1 == nil {
30
+ return l2
31
+ }
32
+ if l2 == nil {
33
+ return l1
34
+ }
35
+ ret := &ListNode{}
36
+ if l1.Val <= l2.Val {
37
+ ret = l1
38
+ ret.Next = mergeTwoLists1 (l1.Next , l2)
39
+ } else {
40
+ ret = l2
41
+ ret.Next = mergeTwoLists1 (l1, l2.Next )
42
+ }
43
+ return ret
44
+ }
45
+ ```
46
+
47
+ #### ** Golang 2**
48
+ ``` go
49
+ func mergeTwoLists2 (l1 *ListNode , l2 *ListNode ) *ListNode {
50
+ head := &ListNode{}
51
+ ret := head
52
+ for l1 != nil && l2 != nil {
53
+ if l1.Val < l2.Val {
54
+ ret.Next = l1
55
+ l1 = l1.Next
56
+ } else {
57
+ ret.Next = l2
58
+ l2 = l2.Next
59
+ }
60
+ ret = ret.Next
61
+ }
62
+ if l1 != nil {
63
+ ret.Next = l1
64
+ }
65
+ if l2 != nil {
66
+ ret.Next = l2
67
+ }
68
+ return head.Next
69
+ }
70
+ ```
71
+
72
+ <!-- tabs:end -->
Original file line number Diff line number Diff line change
1
+ package leetcode
2
+
3
+ /*
4
+ * @lc app=leetcode.cn id=21 lang=golang
5
+ *
6
+ * [21] 合并两个有序链表
7
+ */
8
+
9
+ // @lc code=start
10
+ /**
11
+ * Definition for singly-linked list.
12
+ */
13
+
14
+ func mergeTwoLists1 (l1 * ListNode , l2 * ListNode ) * ListNode {
15
+ if l1 == nil {
16
+ return l2
17
+ }
18
+ if l2 == nil {
19
+ return l1
20
+ }
21
+ ret := & ListNode {}
22
+ if l1 .Val <= l2 .Val {
23
+ ret = l1
24
+ ret .Next = mergeTwoLists1 (l1 .Next , l2 )
25
+ } else {
26
+ ret = l2
27
+ ret .Next = mergeTwoLists1 (l1 , l2 .Next )
28
+ }
29
+ return ret
30
+ }
31
+
32
+ func mergeTwoLists2 (l1 * ListNode , l2 * ListNode ) * ListNode {
33
+ head := & ListNode {}
34
+ ret := head
35
+ for l1 != nil && l2 != nil {
36
+ if l1 .Val < l2 .Val {
37
+ ret .Next = l1
38
+ l1 = l1 .Next
39
+ } else {
40
+ ret .Next = l2
41
+ l2 = l2 .Next
42
+ }
43
+ ret = ret .Next
44
+ }
45
+ if l1 != nil {
46
+ ret .Next = l1
47
+ }
48
+ if l2 != nil {
49
+ ret .Next = l2
50
+ }
51
+ return head .Next
52
+ }
53
+
54
+ // @lc code=end
You can’t perform that action at this time.
0 commit comments