Skip to content

Commit fad7de8

Browse files
committed
🐱(array): 167. 两数之和 II - 输入有序数组
1 parent c29ceee commit fad7de8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

docs/data-structure/array/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,6 +1255,35 @@ class Solution(object):
12551255
return [i + 1, j + 1]
12561256
```
12571257

1258+
- 时间复杂度 `O(n)`
1259+
- 空间复杂度 `O(1)`
1260+
1261+
### 解二:二分查找
1262+
1263+
```python
1264+
class Solution:
1265+
def twoSum(self, numbers: List[int], target: int) -> List[int]:
1266+
length = len(numbers)
1267+
i = 0
1268+
while i < length:
1269+
j = i + 1
1270+
target_j = target - numbers[i]
1271+
left = j
1272+
right = length - 1
1273+
while left <= right:
1274+
mid = (left + right) // 2
1275+
if numbers[mid] == target_j:
1276+
return [i + 1, mid + 1]
1277+
elif numbers[mid] < target_j:
1278+
left = mid + 1
1279+
else:
1280+
right = mid - 1
1281+
i += 1
1282+
```
1283+
1284+
- 时间复杂度 `O(nlogn)`
1285+
- 空间复杂度:`O(1)`
1286+
12581287
## 169. 多数元素
12591288

12601289
[原题链接](https://leetcode-cn.com/problems/majority-element/)

0 commit comments

Comments
 (0)