File tree 5 files changed +154
-2
lines changed
solution/1000-1099/1099.Two Sum Less Than K
5 files changed +154
-2
lines changed Original file line number Diff line number Diff line change 40
40
41
41
<!-- 这里可写通用的实现逻辑 -->
42
42
43
+ 先进行排序,再用双指针 ` low ` 、` high ` 分别指向排序数组的首尾,遍历获取满足条件的和 ` nums[low] + nums[high] ` 并求最大和。
44
+
43
45
<!-- tabs:start -->
44
46
45
47
### ** Python3**
46
48
47
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
48
50
49
51
``` python
50
-
52
+ class Solution :
53
+ def twoSumLessThanK (self , nums : List[int ], k : int ) -> int :
54
+ nums.sort()
55
+ low, high = 0 , len (nums) - 1
56
+ res = - 1
57
+ while low < high:
58
+ val = nums[low] + nums[high]
59
+ if val < k:
60
+ res = max (res, val)
61
+ low += 1
62
+ else :
63
+ high -= 1
64
+ return res
51
65
```
52
66
53
67
### ** Java**
54
68
55
69
<!-- 这里可写当前语言的特殊实现逻辑 -->
56
70
57
71
``` java
72
+ class Solution {
73
+ public int twoSumLessThanK (int [] nums , int k ) {
74
+ Arrays . sort(nums);
75
+ int low = 0 , high = nums. length - 1 ;
76
+ int res = - 1 ;
77
+ while (low < high) {
78
+ int val = nums[low] + nums[high];
79
+ if (val < k) {
80
+ res = Math . max(res, val);
81
+ ++ low;
82
+ } else {
83
+ -- high;
84
+ }
85
+ }
86
+ return res;
87
+ }
88
+ }
89
+ ```
58
90
91
+ ### ** C++**
92
+
93
+ ``` cpp
94
+ class Solution {
95
+ public:
96
+ int twoSumLessThanK(vector<int >& nums, int k) {
97
+ sort(nums.begin(), nums.end());
98
+ int low = 0, high = nums.size() - 1;
99
+ int res = -1;
100
+ while (low < high) {
101
+ int val = nums[ low] + nums[ high] ;
102
+ if (val < k) {
103
+ res = max(res, val);
104
+ ++low;
105
+ } else {
106
+ --high;
107
+ }
108
+ }
109
+ return res;
110
+ }
111
+ };
59
112
```
60
113
61
114
### **...**
Original file line number Diff line number Diff line change @@ -43,13 +43,64 @@ In this case it's not possible to get a pair sum less that 15.
43
43
### ** Python3**
44
44
45
45
``` python
46
-
46
+ class Solution :
47
+ def twoSumLessThanK (self , nums : List[int ], k : int ) -> int :
48
+ nums.sort()
49
+ low, high = 0 , len (nums) - 1
50
+ res = - 1
51
+ while low < high:
52
+ val = nums[low] + nums[high]
53
+ if val < k:
54
+ res = max (res, val)
55
+ low += 1
56
+ else :
57
+ high -= 1
58
+ return res
47
59
```
48
60
49
61
### ** Java**
50
62
51
63
``` java
64
+ class Solution {
65
+ public int twoSumLessThanK (int [] nums , int k ) {
66
+ Arrays . sort(nums);
67
+ int low = 0 , high = nums. length - 1 ;
68
+ int res = - 1 ;
69
+ while (low < high) {
70
+ int val = nums[low] + nums[high];
71
+ if (val < k) {
72
+ res = Math . max(res, val);
73
+ ++ low;
74
+ } else {
75
+ -- high;
76
+ }
77
+ }
78
+ return res;
79
+ }
80
+ }
81
+ ```
52
82
83
+ ### ** C++**
84
+
85
+ ``` cpp
86
+ class Solution {
87
+ public:
88
+ int twoSumLessThanK(vector<int >& nums, int k) {
89
+ sort(nums.begin(), nums.end());
90
+ int low = 0, high = nums.size() - 1;
91
+ int res = -1;
92
+ while (low < high) {
93
+ int val = nums[ low] + nums[ high] ;
94
+ if (val < k) {
95
+ res = max(res, val);
96
+ ++low;
97
+ } else {
98
+ --high;
99
+ }
100
+ }
101
+ return res;
102
+ }
103
+ };
53
104
```
54
105
55
106
### **...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int twoSumLessThanK (vector<int >& nums, int k) {
4
+ sort (nums.begin (), nums.end ());
5
+ int low = 0 , high = nums.size () - 1 ;
6
+ int res = -1 ;
7
+ while (low < high) {
8
+ int val = nums[low] + nums[high];
9
+ if (val < k) {
10
+ res = max (res, val);
11
+ ++low;
12
+ } else {
13
+ --high;
14
+ }
15
+ }
16
+ return res;
17
+ }
18
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int twoSumLessThanK (int [] nums , int k ) {
3
+ Arrays .sort (nums );
4
+ int low = 0 , high = nums .length - 1 ;
5
+ int res = -1 ;
6
+ while (low < high ) {
7
+ int val = nums [low ] + nums [high ];
8
+ if (val < k ) {
9
+ res = Math .max (res , val );
10
+ ++low ;
11
+ } else {
12
+ --high ;
13
+ }
14
+ }
15
+ return res ;
16
+ }
17
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def twoSumLessThanK (self , nums : List [int ], k : int ) -> int :
3
+ nums .sort ()
4
+ low , high = 0 , len (nums ) - 1
5
+ res = - 1
6
+ while low < high :
7
+ val = nums [low ] + nums [high ]
8
+ if val < k :
9
+ res = max (res , val )
10
+ low += 1
11
+ else :
12
+ high -= 1
13
+ return res
You can’t perform that action at this time.
0 commit comments