Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 69b2fc7

Browse files
ybian19azl397985856
authored andcommitted
feat: 167.two-sum-ii-input-array-is-sorted add Python3 implement... (azl397985856#101)
* feat: 167.two-sum-ii-input-array-is-sorted add Python3 implementation * Update 167.two-sum-ii-input-array-is-sorted.md
1 parent 11cebd6 commit 69b2fc7

File tree

1 file changed

+39
-15
lines changed

1 file changed

+39
-15
lines changed

‎problems/167.two-sum-ii-input-array-is-sorted.md‎

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
2828
由于题目没有对空间复杂度有求,用一个hashmap 存储已经访问过的数字即可。
2929

3030
假如题目空间复杂度有要求,由于数组是有序的,只需要双指针即可。一个left指针,一个right指针,
31-
如果left + right 值 大于target 则 right左移动, 否则left右移,代码比较简单, 不贴了
31+
如果left + right 值 大于target 则 right左移动, 否则left右移,代码见下方python code
3232

3333
> 如果数组无序,需要先排序(从这里也可以看出排序是多么重要的操作)
3434
@@ -40,6 +40,10 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
4040

4141
## 代码
4242

43+
* 语言支持:JS,Python
44+
45+
Javascript Code:
46+
4347
```js
4448
/*
4549
* @lc app=leetcode id=167 lang=javascript
@@ -56,25 +60,25 @@ Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
5660
*
5761
* Given an array of integers that is already sorted in ascending order, find
5862
* two numbers such that they add up to a specific target number.
59-
*
63+
*
6064
* The function twoSum should return indices of the two numbers such that they
6165
* add up to the target, where index1 must be less than index2.
62-
*
66+
*
6367
* Note:
64-
*
65-
*
68+
*
69+
*
6670
* Your returned answers (both index1 and index2) are not zero-based.
6771
* You may assume that each input would have exactly one solution and you may
6872
* not use the same element twice.
69-
*
70-
*
73+
*
74+
*
7175
* Example:
72-
*
73-
*
76+
*
77+
*
7478
* Input: numbers = [2,7,11,15], target = 9
7579
* Output: [1,2]
7680
* Explanation: The sum of 2 and 7 is 9. Therefore index1 = 1, index2 = 2.
77-
*
81+
*
7882
*/
7983
/**
8084
* @param {number[]} numbers
@@ -89,13 +93,33 @@ var twoSum = function(numbers, target) {
8993
if (visited[target - element] !== void 0) {
9094
return [visited[target - element], index + 1]
9195
}
92-
visited[element] = index + 1;
96+
visited[element] = index + 1;
9397
}
9498
return [];
9599
};
96-
97-
98100
```
99101

100-
101-
102+
Python Code:
103+
104+
```python
105+
class Solution:
106+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
107+
visited = {}
108+
for index, number in enumerate(numbers):
109+
if target - number in visited:
110+
return [visited[target-number], index+1]
111+
else:
112+
visited[number] = index + 1
113+
114+
# 双指针思路实现
115+
class Solution:
116+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
117+
left, right = 0, len(numbers) - 1
118+
while left < right:
119+
if numbers[left] + numbers[right] < target:
120+
left += 1
121+
if numbers[left] + numbers[right] > target:
122+
right -= 1
123+
if numbers[left] + numbers[right] == target:
124+
return [left+1, right+1]
125+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /