Skip to content

Commit feaf25a

Browse files
committed
feat: commit solution 167
1 parent 1399cd9 commit feaf25a

File tree

6 files changed

+118
-1
lines changed

6 files changed

+118
-1
lines changed

_sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
2424
- [118.杨辉三角](solution/100-199/0118.pascals-triangle/)
2525
- [125.验证回文串](solution/100-199/0125.valid-palindrome/)
26+
- [167.两数之和 II - 输入有序数组](solution/100-199/0167.two-sum-ii-input-array-is-sorted/)
2627
- [169.多数元素](solution/100-199/0169.majority-element/)
2728
- [217.存在重复元素](solution/200-299/0217.contains-duplicate/)
2829
- [219.存在重复元素 II](solution/200-299/0219.contains-duplicate-ii/)

index-tags.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
1414
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
1515
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
16+
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
1617
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
1718
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
1819
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
@@ -61,8 +62,10 @@
6162
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
6263
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
6364
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
65+
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
6466
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
6567

68+
6669
#### **链表**
6770

6871
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
@@ -75,6 +78,7 @@
7578
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
7679
| --- | --- | --- | --- | --- |
7780
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
81+
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
7882

7983
#### **排序**
8084

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
| [155](https://leetcode-cn.com/problems/min-stack) | [最小栈](/solution/100-199/0155.min-stack/) | ``,`设计` | <font color=green>简单</font> |
4646
| [157](https://leetcode-cn.com/problems/read-n-characters-given-read4) | [用 read4 读取 n 个字符](/solution/100-199/0157.read-n-characters-given-read4/) | `字符串` | <font color=green>简单</font> |
4747
| [160](https://leetcode-cn.com/problems/intersection-of-two-linked-lists) | [相交链表](/solution/100-199/0160.intersection-of-two-linked-lists/) | `链表` | <font color=green>简单</font> |
48-
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> |
48+
| [167](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted) | [两数之和 ii - 输入有序数组](/solution/100-199/0167.two-sum-ii---input-array-is-sorted/) | `数组`,`双指针`,`二分查找` | <font color=green>简单</font> ||
4949
| [168](https://leetcode-cn.com/problems/excel-sheet-column-title) | [excel表列名称](/solution/100-199/0168.excel-sheet-column-title/) | `数学` | <font color=green>简单</font> |
5050
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
5151
| [170](https://leetcode-cn.com/problems/two-sum-iii-data-structure-design) | [两数之和 iii - 数据结构设计](/solution/100-199/0170.two-sum-iii---data-structure-design/) | `设计`,`哈希表` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# [167.两数之和 II - 输入有序数组](https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个已按照<strong><em>升序排列</em>&nbsp;</strong>的有序数组,找到两个数使得它们相加之和等于目标数。</p>
7+
8+
<p>函数应该返回这两个下标值<em> </em>index1 和 index2,其中 index1&nbsp;必须小于&nbsp;index2<em>。</em></p>
9+
10+
<p><strong>说明:</strong></p>
11+
12+
<ul>
13+
<li>返回的下标值(index1 和 index2)不是从零开始的。</li>
14+
<li>你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。</li>
15+
</ul>
16+
17+
<p><strong>示例:</strong></p>
18+
19+
<pre><strong>输入:</strong> numbers = [2, 7, 11, 15], target = 9
20+
<strong>输出:</strong> [1,2]
21+
<strong>解释:</strong> 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。</pre>
22+
</div>
23+
24+
### 解题思路
25+
26+
1. 双指针
27+
28+
### 代码实现
29+
30+
<!-- tabs:start -->
31+
32+
#### **Golang**
33+
```go
34+
func twoSum(numbers []int, target int) []int {
35+
left := 0
36+
right := len(numbers) - 1
37+
for left != right {
38+
if numbers[left]+numbers[right] == target {
39+
break
40+
}
41+
if numbers[left]+numbers[right] > target {
42+
right--
43+
}
44+
if numbers[left]+numbers[right] < target {
45+
left++
46+
}
47+
}
48+
return []int{left + 1, right + 1}
49+
}
50+
```
51+
52+
53+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=167 lang=golang
5+
*
6+
* [167] 两数之和 II - 输入有序数组
7+
*/
8+
9+
// @lc code=start
10+
func twoSum(numbers []int, target int) []int {
11+
left := 0
12+
right := len(numbers) - 1
13+
for left != right {
14+
if numbers[left]+numbers[right] == target {
15+
break
16+
}
17+
if numbers[left]+numbers[right] > target {
18+
right--
19+
}
20+
if numbers[left]+numbers[right] < target {
21+
left++
22+
}
23+
}
24+
return []int{left + 1, right + 1}
25+
}
26+
27+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestTwoSum(t *testing.T) {
8+
9+
var target int
10+
var nums []int
11+
var ret []int
12+
13+
nums = []int{2, 7, 11, 15}
14+
target = 9
15+
ret = []int{1, 2}
16+
17+
for k, v := range twoSum(nums, target) {
18+
if v != ret[k] {
19+
t.Fatalf("case fails: %v\n", ret)
20+
}
21+
}
22+
23+
nums = []int{2, 7, 11, 15}
24+
target = 13
25+
ret = []int{1, 3}
26+
27+
for k, v := range twoSum(nums, target) {
28+
if v != ret[k] {
29+
t.Fatalf("case fails: %v\n", ret)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)