Skip to content

Commit 92ac75c

Browse files
committedJun 18, 2020
feat: commit solution
1 parent ebb4a9a commit 92ac75c

File tree

17 files changed

+410
-43
lines changed

17 files changed

+410
-43
lines changed
 

‎README.md

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
**LeetCode** 题解
33

44
[![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)
85
[![Travis](https://www.travis-ci.org/lxlxw/leetcode.svg?branch=master)](https://www.travis-ci.org/lxlxw/leetcode)
96
[![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)
108
[![HitCount](http://hits.dwyl.com/lxlxw/leetcode.svg)](http://hits.dwyl.com/lxlxw/leetcode)
119

1210
[ [中文](http://leetcode.xwlin.com)
1311
| [English](http://leetcode.xwlin.com/#/en/)
1412
]
1513

16-
😀 使用`Golang`/`Python`/`PHP`等语言实现LeetCode题解(持续更新中...)
14+
😀 使用`Golang`语言实现LeetCode题解(持续更新中...)
1715

1816
## 项目简介
1917

@@ -24,7 +22,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
2422
- 可根据题目难易度与题型标签进行`快速索引`
2523
- 包含leetcode问题链接的`说明``示例`
2624
- 包含leetcode`解题思路`和具体的`代码实现`
27-
- 包含单元测试,已提交的问题均已通过leetcode各自的测试用例。
25+
- 包含完整的单元测试,已提交的问题均已通过leetcode各自的测试用例。
2826
- 可以直接通过编辑页面实现`Pull requests`
2927

3028

‎_sidebar.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
- [9.回文数 ✅](solution/1-99/0009.palindrome-number/)
1414
- [13.罗马数字转整数 ✅](solution/1-99/0013.roman-to-integer/)
1515
- [20.有效的括号 ✅](solution/1-99/0020.valid-parentheses/)
16+
- [21.合并两个有序链表 ✅](solution/1-99/0021.merge-two-sorted-lists/)
1617
- [26.删除排序数组中的重复项 ✅](solution/1-99/0026.remove-duplicates-from-sorted-array/)
18+
- [27.移除元素 ✅](solution/1-99/0027.remove-element/)
1719

1820

1921

‎index-tags.md

+8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
| --- | --- | --- | --- | --- |
2424
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
2525
| [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> ||
2627

2728
#### **哈希表**
2829

@@ -38,6 +39,12 @@
3839

3940
#### ****
4041

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+
4148
#### **排序**
4249

4350
#### **队列**
@@ -53,6 +60,7 @@
5360
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
5461
| --- | --- | --- | --- | --- |
5562
| [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> ||
5664

5765
#### **递归**
5866

‎index-type.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
1414
| [14](https://leetcode-cn.com/problems/longest-common-prefix) | [最长公共前缀](/solution/1-99/0014.longest-common-prefix/) | `字符串` | <font color=green>简单</font> |
1515
| [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> ||
1717
| [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> ||
1919
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> |
2020
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> |
2121
| [38](https://leetcode-cn.com/problems/count-and-say) | [外观数列](/solution/1-99/0038.count-and-say/) | `字符串` | <font color=green>简单</font> |

‎solution/1-99/0001.two-sum/README.md

-4
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ func twoSum(nums []int, target int) []int {
3737
return []int{}
3838
}
3939
```
40-
#### **Java**
4140

42-
#### **Python**
43-
44-
#### **PHP**
4541

4642
<!-- tabs:end -->

‎solution/1-99/0002.add-two-numbers/README.md

-5
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,5 @@ func sk() error {
2929
return nil
3030
}
3131
```
32-
#### **Java**
33-
34-
#### **Python**
35-
36-
#### **PHP**
3732

3833
<!-- tabs:end -->

‎solution/1-99/0007.reverse-integer/README.md

-5
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,5 @@ func reverse(x int) int {
5353
return ret
5454
}
5555
```
56-
#### **Java**
57-
58-
#### **Python**
59-
60-
#### **PHP**
6156

6257
<!-- tabs:end -->

‎solution/1-99/0009.palindrome-number/README.md

-6
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,4 @@ func isPalindrome(x int) bool {
5151
return true
5252
}
5353
```
54-
#### **Java**
55-
56-
#### **Python**
57-
58-
#### **PHP**
59-
6054
<!-- tabs:end -->

‎solution/1-99/0013.roman-to-integer/README.md

+1-6
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ M 1000</pre>
6161
1. 先匹配特殊的字符串,匹配到的话`ret`累加
6262
2. 遍历字符串,如果能在`romanMap`中找到的话`ret`累加
6363

64-
?> 该题解不是最优解!
64+
?> 说明:该题解不是最优解!
6565

6666
### 代码实现
6767

@@ -107,10 +107,5 @@ func romanToInt(s string) int {
107107
return ret
108108
}
109109
```
110-
#### **Java**
111-
112-
#### **Python**
113-
114-
#### **PHP**
115110

116111
<!-- tabs:end -->

‎solution/1-99/0020.valid-parentheses/README.md

-5
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,5 @@ func isValid(s string) bool {
8484
return stack.Len() == 0
8585
}
8686
```
87-
#### **Java**
88-
89-
#### **Python**
90-
91-
#### **PHP**
9287

9388
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# [21.合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>将两个升序链表合并为一个新的 <strong>升序</strong> 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。&nbsp;</p>
7+
8+
<p>&nbsp;</p>
9+
10+
<p><strong>示例:</strong></p>
11+
12+
<pre><strong>输入:</strong>1-&gt;2-&gt;4, 1-&gt;3-&gt;4
13+
<strong>输出:</strong>1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;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 numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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

0 commit comments

Comments
 (0)
Please sign in to comment.