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 63a98d4

Browse files
committed
feat: add python and java solutions to leetcode problem: No.1099
1 parent 4ad888b commit 63a98d4

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed

‎solution/1000-1099/1099.Two Sum Less Than K/README.md‎

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,75 @@
4040

4141
<!-- 这里可写通用的实现逻辑 -->
4242

43+
先进行排序,再用双指针 `low``high` 分别指向排序数组的首尾,遍历获取满足条件的和 `nums[low] + nums[high]` 并求最大和。
44+
4345
<!-- tabs:start -->
4446

4547
### **Python3**
4648

4749
<!-- 这里可写当前语言的特殊实现逻辑 -->
4850

4951
```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
5165
```
5266

5367
### **Java**
5468

5569
<!-- 这里可写当前语言的特殊实现逻辑 -->
5670

5771
```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+
```
5890

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+
};
59112
```
60113
61114
### **...**

‎solution/1000-1099/1099.Two Sum Less Than K/README_EN.md‎

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,64 @@ In this case it's not possible to get a pair sum less that 15.
4343
### **Python3**
4444

4545
```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
4759
```
4860

4961
### **Java**
5062

5163
```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+
```
5282

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+
};
53104
```
54105
55106
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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

0 commit comments

Comments
(0)

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