Skip to content

Commit e2d3288

Browse files
committed
add 704,206
1 parent 9b062a8 commit e2d3288

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
| [14](./algorithms/0014) | [14. 最长公共前缀](https://leetcode-cn.com/problems/longest-common-prefix/) | [Go](./algorithms/0014/main.go) | 简单 |
1818
| [79](./algorithms/0079) | [79. 单词搜索](https://leetcode-cn.com/problems/word-search/) | [Go](./algorithms/0079/main.go) | 中等 |
1919
| [146](./algorithms/0146) | [146. LRU 缓存机制](https://leetcode-cn.com/problems/lru-cache/) | [Go](./algorithms/0146/main.go) | 中等 |
20+
| [206](./algorithms/0206) | [206. 反转链表](https://leetcode-cn.com/problems/reverse-linked-list/) | [Go](./algorithms/0206/main.go) | 简单 |
2021
| [232](./algorithms/0232) | [232. 用栈实现队列](https://leetcode-cn.com/problems/implement-queue-using-stacks/) | [Go](./algorithms/0232/main.go) | 简单 |
2122
| [263](./algorithms/0263) | [263. 丑数](https://leetcode-cn.com/problems/ugly-number/) | [Go](./algorithms/0263/main.go) | 简单 |
23+
| [704](./algorithms/0704) | [704. 二分查找](https://leetcode-cn.com/problems/binary-search/) | [Go](./algorithms/0704/main.go) | 简单 |
2224
| [offer47](./algorithms/offer47) | [剑指 Offer 47. 礼物的最大价值](https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof/) | [Go](./algorithms/offer47/main.go) | 中等 |
2325

2426

algorithms/0206/README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#### [206. 反转链表](https://leetcode-cn.com/problems/reverse-linked-list/)
2+
3+
难度简单
4+
5+
给你单链表的头节点 `head` ,请你反转链表,并返回反转后的链表。
6+
7+
8+
9+
**示例 1:**
10+
11+
![img](https://assets.leetcode.com/uploads/2021/02/19/rev1ex1.jpg)
12+
13+
```
14+
输入:head = [1,2,3,4,5]
15+
输出:[5,4,3,2,1]
16+
```
17+
18+
**示例 2:**
19+
20+
![img](https://assets.leetcode.com/uploads/2021/02/19/rev1ex2.jpg)
21+
22+
```
23+
输入:head = [1,2]
24+
输出:[2,1]
25+
```
26+
27+
**示例 3:**
28+
29+
```
30+
输入:head = []
31+
输出:[]
32+
```
33+
34+
35+
36+
**提示:**
37+
38+
- 链表中节点的数目范围是 `[0, 5000]`
39+
- `-5000 <= Node.val <= 5000`
40+
41+
42+
43+
**进阶:**链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?

algorithms/0206/main.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package _0206
2+
3+
/**
4+
* Definition for singly-linked list.
5+
* type ListNode struct {
6+
* Val int
7+
* Next *ListNode
8+
* }
9+
*/
10+
11+
type ListNode struct {
12+
Val int
13+
Next *ListNode
14+
}
15+
16+
func reverseList(head *ListNode) *ListNode {
17+
var prev *ListNode
18+
curr := head
19+
for curr != nil {
20+
next := curr.Next
21+
curr.Next = prev
22+
prev = curr
23+
curr = next
24+
}
25+
return prev
26+
}
27+
28+
func reverseListRecursion(head *ListNode) *ListNode {
29+
return nil
30+
}

algorithms/0704/README.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#### [704. 二分查找](https://leetcode-cn.com/problems/binary-search/)
2+
3+
难度简单
4+
5+
给定一个 `n` 个元素有序的(升序)整型数组 `nums` 和一个目标值 `target` ,写一个函数搜索 `nums` 中的 `target`,如果目标值存在返回下标,否则返回 `-1`
6+
7+
8+
**示例 1:**
9+
10+
```
11+
输入: nums = [-1,0,3,5,9,12], target = 9
12+
输出: 4
13+
解释: 9 出现在 nums 中并且下标为 4
14+
```
15+
16+
**示例 2:**
17+
18+
```
19+
输入: nums = [-1,0,3,5,9,12], target = 2
20+
输出: -1
21+
解释: 2 不存在 nums 中因此返回 -1
22+
```
23+
24+
25+
26+
**提示:**
27+
28+
1. 你可以假设 `nums` 中的所有元素是不重复的。
29+
2. `n` 将在 `[1, 10000]`之间。
30+
3. `nums` 的每个元素都将在 `[-9999, 9999]`之间。

algorithms/0704/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package _0704
2+
3+
func search(nums []int, target int) int {
4+
left, right := 0, len(nums)-1
5+
for left <= right {
6+
mid := (right + left) / 2
7+
if target == nums[mid] {
8+
return mid
9+
} else if target > nums[mid] {
10+
left = mid + 1
11+
} else {
12+
right = mid - 1
13+
}
14+
}
15+
return -1
16+
}

0 commit comments

Comments
 (0)