File tree 5 files changed +131
-41
lines changed
solution/0100-0199/0167.Two Sum II - Input array is sorted
5 files changed +131
-41
lines changed Original file line number Diff line number Diff line change 26
26
27
27
<!-- 这里可写通用的实现逻辑 -->
28
28
29
+ 双指针解决。
30
+
29
31
<!-- tabs:start -->
30
32
31
33
### ** Python3**
32
34
33
35
<!-- 这里可写当前语言的特殊实现逻辑 -->
34
36
35
37
``` python
36
-
38
+ class Solution :
39
+ def twoSum (self , numbers : List[int ], target : int ) -> List[int ]:
40
+ low, high = 0 , len (numbers) - 1
41
+ while low <= high:
42
+ if numbers[low] + numbers[high] == target:
43
+ return [low + 1 , high + 1 ]
44
+ if numbers[low] + numbers[high] < target:
45
+ low += 1
46
+ else :
47
+ high -= 1
48
+ return [- 1 , - 1 ]
37
49
```
38
50
39
51
### ** Java**
40
52
41
53
<!-- 这里可写当前语言的特殊实现逻辑 -->
42
54
43
55
``` java
56
+ class Solution {
57
+ public int [] twoSum (int [] numbers , int target ) {
58
+ int low = 0 , high = numbers. length - 1 ;
59
+ while (low <= high) {
60
+ if (numbers[low] + numbers[high] == target) {
61
+ return new int []{low + 1 , high + 1 };
62
+ }
63
+ if (numbers[low] + numbers[high] < target) {
64
+ ++ low;
65
+ } else {
66
+ -- high;
67
+ }
68
+ }
69
+ return new int []{- 1 , - 1 };
70
+ }
71
+ }
72
+ ```
44
73
74
+ ### ** C++**
75
+
76
+ ``` cpp
77
+ class Solution {
78
+ public:
79
+ vector<int > twoSum(vector<int >& numbers, int target) {
80
+ int low = 0, high = numbers.size() - 1;
81
+ while (low <= high) {
82
+ if (numbers[ low] + numbers[ high] == target) {
83
+ return {low + 1, high + 1};
84
+ }
85
+ if (numbers[ low] + numbers[ high] < target) {
86
+ ++low;
87
+ } else {
88
+ --high;
89
+ }
90
+ }
91
+ return {-1, -1};
92
+ }
93
+ };
45
94
```
46
95
47
96
### **...**
Original file line number Diff line number Diff line change 32
32
### ** Python3**
33
33
34
34
``` python
35
-
35
+ class Solution :
36
+ def twoSum (self , numbers : List[int ], target : int ) -> List[int ]:
37
+ low, high = 0 , len (numbers) - 1
38
+ while low <= high:
39
+ if numbers[low] + numbers[high] == target:
40
+ return [low + 1 , high + 1 ]
41
+ if numbers[low] + numbers[high] < target:
42
+ low += 1
43
+ else :
44
+ high -= 1
45
+ return [- 1 , - 1 ]
36
46
```
37
47
38
48
### ** Java**
39
49
40
50
``` java
51
+ class Solution {
52
+ public int [] twoSum (int [] numbers , int target ) {
53
+ int low = 0 , high = numbers. length - 1 ;
54
+ while (low <= high) {
55
+ if (numbers[low] + numbers[high] == target) {
56
+ return new int []{low + 1 , high + 1 };
57
+ }
58
+ if (numbers[low] + numbers[high] < target) {
59
+ ++ low;
60
+ } else {
61
+ -- high;
62
+ }
63
+ }
64
+ return new int []{- 1 , - 1 };
65
+ }
66
+ }
67
+ ```
41
68
69
+ ### ** C++**
70
+
71
+ ``` cpp
72
+ class Solution {
73
+ public:
74
+ vector<int > twoSum(vector<int >& numbers, int target) {
75
+ int low = 0, high = numbers.size() - 1;
76
+ while (low <= high) {
77
+ if (numbers[ low] + numbers[ high] == target) {
78
+ return {low + 1, high + 1};
79
+ }
80
+ if (numbers[ low] + numbers[ high] < target) {
81
+ ++low;
82
+ } else {
83
+ --high;
84
+ }
85
+ }
86
+ return {-1, -1};
87
+ }
88
+ };
42
89
```
43
90
44
91
### **...**
Original file line number Diff line number Diff line change 1
- // 先看target是否是偶数,
2
- // 偶数的话先排除numbers[i] == numbers[i+1] == target/2
3
- // 然后set查找
4
- // 存在的话 再折半查找定位
5
- class Solution {
1
+ class Solution {
6
2
public:
7
3
vector<int > twoSum (vector<int >& numbers, int target) {
8
- set<int > s (numbers.begin (), numbers.end ()) ;
9
-
10
- int t = target ;
11
- if (!(target & 1 ))
12
- t /= 2 ;
13
-
14
- for (int i = 0 ; i < numbers.size ()-1 ; ++i)
15
- {
16
- if (numbers[i] == t && numbers[i] == numbers[i+1 ])
17
- return {i+1 , i+2 } ;
18
-
19
- int tar = target - numbers[i] ;
20
- if (s.find (tar) != s.end ())
21
- {
22
- int l = i+1 , r = numbers.size (), mid ;
23
- while (l < r)
24
- {
25
- mid = (l+r) >> 1 ;
26
- if (numbers[mid] > tar)
27
- r = mid ;
28
- else if (numbers[mid] < tar)
29
- l = mid+1 ;
30
- else
31
- break ;
32
- }
33
- return {i+1 , mid+1 } ;
4
+ int low = 0 , high = numbers.size () - 1 ;
5
+ while (low <= high) {
6
+ if (numbers[low] + numbers[high] == target) {
7
+ return {low + 1 , high + 1 };
8
+ }
9
+ if (numbers[low] + numbers[high] < target) {
10
+ ++low;
11
+ } else {
12
+ --high;
34
13
}
35
14
}
36
-
37
- return {0 , 0 } ;
15
+ return {-1 , -1 };
38
16
}
39
17
};
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int [] twoSum (int [] numbers , int target ) {
3
- int l = 0 , r = numbers .length - 1 ;
4
-
5
- while (numbers [l ]+numbers [r ] != target ) {
6
- while (numbers [l ] + numbers [r ] > target ) r --;
7
- while (numbers [l ] + numbers [r ] < target ) l ++;
3
+ int low = 0 , high = numbers .length - 1 ;
4
+ while (low <= high ) {
5
+ if (numbers [low ] + numbers [high ] == target ) {
6
+ return new int []{low + 1 , high + 1 };
7
+ }
8
+ if (numbers [low ] + numbers [high ] < target ) {
9
+ ++low ;
10
+ } else {
11
+ --high ;
12
+ }
8
13
}
9
- return new int [] { l + 1 , r + 1 };
14
+ return new int []{- 1 , - 1 };
10
15
}
11
16
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def twoSum (self , numbers : List [int ], target : int ) -> List [int ]:
3
+ low , high = 0 , len (numbers ) - 1
4
+ while low <= high :
5
+ if numbers [low ] + numbers [high ] == target :
6
+ return [low + 1 , high + 1 ]
7
+ if numbers [low ] + numbers [high ] < target :
8
+ low += 1
9
+ else :
10
+ high -= 1
11
+ return [- 1 , - 1 ]
You can’t perform that action at this time.
0 commit comments