52
52
``` python
53
53
class Solution :
54
54
def threeSumSmaller (self , nums : List[int ], target : int ) -> int :
55
- def threeSumSmaller (nums , start , end , target ):
56
- count = 0
57
- while start < end:
58
- if nums[start] + nums[end] < target:
59
- count += (end - start)
60
- start += 1
61
- else :
62
- end -= 1
63
- return count
64
-
65
55
nums.sort()
66
- n, count = len (nums), 0
67
- for i in range (n - 2 ):
68
- count += threeSumSmaller(nums, i + 1 , n - 1 , target - nums[i])
69
- return count
56
+ ans, n = 0 , len (nums)
57
+ for i in range (n):
58
+ j, k = i + 1 , n - 1
59
+ while j < k:
60
+ s = nums[i] + nums[j] + nums[k]
61
+ if s >= target:
62
+ k -= 1
63
+ else :
64
+ ans += k - j
65
+ j += 1
66
+ return ans
70
67
```
71
68
72
69
### ** Java**
@@ -75,26 +72,71 @@ class Solution:
75
72
class Solution {
76
73
public int threeSumSmaller (int [] nums , int target ) {
77
74
Arrays . sort(nums);
78
- int n = nums. length;
79
- int count = 0 ;
80
- for (int i = 0 ; i < n - 2 ; ++ i) {
81
- count += threeSumSmaller(nums, i + 1 , n - 1 , target - nums[i]);
75
+ int ans = 0 ;
76
+ for (int i = 0 , n = nums. length; i < n; ++ i) {
77
+ int j = i + 1 ;
78
+ int k = n - 1 ;
79
+ while (j < k) {
80
+ int s = nums[i] + nums[j] + nums[k];
81
+ if (s >= target) {
82
+ -- k;
83
+ } else {
84
+ ans += k - j;
85
+ ++ j;
86
+ }
87
+ }
82
88
}
83
- return count ;
89
+ return ans ;
84
90
}
91
+ }
92
+ ```
85
93
86
- private int threeSumSmaller (int [] nums , int start , int end , int target ) {
87
- int count = 0 ;
88
- while (start < end) {
89
- if (nums[start] + nums[end] < target) {
90
- count += (end - start);
91
- ++ start;
92
- } else {
93
- -- end;
94
+ ### ** C++**
95
+
96
+ ``` cpp
97
+ class Solution {
98
+ public:
99
+ int threeSumSmaller(vector<int >& nums, int target) {
100
+ sort(nums.begin(), nums.end());
101
+ int ans = 0;
102
+ for (int i = 0, n = nums.size(); i < n; ++i)
103
+ {
104
+ int j = i + 1, k = n - 1;
105
+ while (j < k)
106
+ {
107
+ int s = nums[ i] + nums[ j] + nums[ k] ;
108
+ if (s >= target) --k;
109
+ else
110
+ {
111
+ ans += k - j;
112
+ ++j;
113
+ }
94
114
}
95
115
}
96
- return count ;
116
+ return ans ;
97
117
}
118
+ };
119
+ ```
120
+
121
+ ### **Go**
122
+
123
+ ```go
124
+ func threeSumSmaller(nums []int, target int) int {
125
+ sort.Ints(nums)
126
+ ans := 0
127
+ for i, n := 0, len(nums); i < n; i++ {
128
+ j, k := i+1, n-1
129
+ for j < k {
130
+ s := nums[i] + nums[j] + nums[k]
131
+ if s >= target {
132
+ k--
133
+ } else {
134
+ ans += k - j
135
+ j++
136
+ }
137
+ }
138
+ }
139
+ return ans
98
140
}
99
141
```
100
142
@@ -107,26 +149,22 @@ class Solution {
107
149
* @return {number}
108
150
*/
109
151
var threeSumSmaller = function (nums , target ) {
110
- let len = nums .length ;
111
- if (len < 3 ) return 0 ;
112
152
nums .sort ((a , b ) => a - b);
113
- let res = 0 ;
114
- for (let i = 0 ; i < len - 2 ; i++ ) {
115
- let left = i + 1 ,
116
- right = len - 1 ;
117
- if (nums[i] + nums[left] + nums[i + 2 ] >= target) break ;
118
- while (left < right) {
119
- if (nums[i] + nums[left] + nums[right] < target) {
120
- res += right - left;
121
- left++ ;
122
- continue ;
153
+ let ans = 0 ;
154
+ for (let i = 0 , n = nums .length ; i < n; ++ i) {
155
+ let j = i + 1 ;
156
+ let k = n - 1 ;
157
+ while (j < k) {
158
+ s = nums[i] + nums[j] + nums[k];
159
+ if (s >= target) {
160
+ -- k;
123
161
} else {
124
- right -- ;
125
- continue ;
162
+ ans += k - j ;
163
+ ++ j ;
126
164
}
127
165
}
128
166
}
129
- return res ;
167
+ return ans ;
130
168
};
131
169
```
132
170
0 commit comments