Skip to content

Commit 1d319de

Browse files
committed
feat: add solutions to lc problems: No.0258,0259
* No.0258.Add Digits * No.0259.3Sum Smaller
1 parent 591c435 commit 1d319de

File tree

10 files changed

+268
-132
lines changed

10 files changed

+268
-132
lines changed

solution/0200-0299/0258.Add Digits/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ class Solution:
5555

5656
```java
5757
class Solution {
58+
5859
public int addDigits(int num) {
5960
return (num - 1) % 9 + 1;
6061
}
6162
}
63+
6264
```
6365

6466
### **C++**
@@ -72,6 +74,17 @@ public:
7274
};
7375
```
7476
77+
### **Go**
78+
79+
```go
80+
func addDigits(num int) int {
81+
if num == 0 {
82+
return 0
83+
}
84+
return (num-1)%9 + 1
85+
}
86+
```
87+
7588
### **...**
7689

7790
```

solution/0200-0299/0258.Add Digits/README_EN.md

+13
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ class Solution:
5151

5252
```java
5353
class Solution {
54+
5455
public int addDigits(int num) {
5556
return (num - 1) % 9 + 1;
5657
}
5758
}
59+
5860
```
5961

6062
### **C++**
@@ -68,6 +70,17 @@ public:
6870
};
6971
```
7072
73+
### **Go**
74+
75+
```go
76+
func addDigits(num int) int {
77+
if num == 0 {
78+
return 0
79+
}
80+
return (num-1)%9 + 1
81+
}
82+
```
83+
7184
### **...**
7285

7386
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func addDigits(num int) int {
2+
if num == 0 {
3+
return 0
4+
}
5+
return (num-1)%9 + 1
6+
}

solution/0200-0299/0259.3Sum Smaller/README.md

+81-43
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,18 @@
3434
```python
3535
class Solution:
3636
def threeSumSmaller(self, nums: List[int], target: int) -> int:
37-
def threeSumSmaller(nums, start, end, target):
38-
count = 0
39-
while start < end:
40-
if nums[start] + nums[end] < target:
41-
count += (end - start)
42-
start += 1
43-
else:
44-
end -= 1
45-
return count
46-
4737
nums.sort()
48-
n, count = len(nums), 0
49-
for i in range(n - 2):
50-
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i])
51-
return count
38+
ans, n = 0, len(nums)
39+
for i in range(n):
40+
j, k = i + 1, n - 1
41+
while j < k:
42+
s = nums[i] + nums[j] + nums[k]
43+
if s >= target:
44+
k -= 1
45+
else:
46+
ans += k - j
47+
j += 1
48+
return ans
5249
```
5350

5451
### **Java**
@@ -59,26 +56,71 @@ class Solution:
5956
class Solution {
6057
public int threeSumSmaller(int[] nums, int target) {
6158
Arrays.sort(nums);
62-
int n = nums.length;
63-
int count = 0;
64-
for (int i = 0; i < n - 2; ++i) {
65-
count += threeSumSmaller(nums, i + 1, n - 1, target - nums[i]);
59+
int ans = 0;
60+
for (int i = 0, n = nums.length; i < n; ++i) {
61+
int j = i + 1;
62+
int k = n - 1;
63+
while (j < k) {
64+
int s = nums[i] + nums[j] + nums[k];
65+
if (s >= target) {
66+
--k;
67+
} else {
68+
ans += k - j;
69+
++j;
70+
}
71+
}
6672
}
67-
return count;
73+
return ans;
6874
}
75+
}
76+
```
6977

70-
private int threeSumSmaller(int[] nums, int start, int end, int target) {
71-
int count = 0;
72-
while (start < end) {
73-
if (nums[start] + nums[end] < target) {
74-
count += (end - start);
75-
++start;
76-
} else {
77-
--end;
78+
### **C++**
79+
80+
```cpp
81+
class Solution {
82+
public:
83+
int threeSumSmaller(vector<int>& nums, int target) {
84+
sort(nums.begin(), nums.end());
85+
int ans = 0;
86+
for (int i = 0, n = nums.size(); i < n; ++i)
87+
{
88+
int j = i + 1, k = n - 1;
89+
while (j < k)
90+
{
91+
int s = nums[i] + nums[j] + nums[k];
92+
if (s >= target) --k;
93+
else
94+
{
95+
ans += k - j;
96+
++j;
97+
}
7898
}
7999
}
80-
return count;
100+
return ans;
81101
}
102+
};
103+
```
104+
105+
### **Go**
106+
107+
```go
108+
func threeSumSmaller(nums []int, target int) int {
109+
sort.Ints(nums)
110+
ans := 0
111+
for i, n := 0, len(nums); i < n; i++ {
112+
j, k := i+1, n-1
113+
for j < k {
114+
s := nums[i] + nums[j] + nums[k]
115+
if s >= target {
116+
k--
117+
} else {
118+
ans += k - j
119+
j++
120+
}
121+
}
122+
}
123+
return ans
82124
}
83125
```
84126

@@ -91,26 +133,22 @@ class Solution {
91133
* @return {number}
92134
*/
93135
var threeSumSmaller = function (nums, target) {
94-
let len = nums.length;
95-
if (len < 3) return 0;
96136
nums.sort((a, b) => a - b);
97-
let res = 0;
98-
for (let i = 0; i < len - 2; i++) {
99-
let left = i + 1,
100-
right = len - 1;
101-
if (nums[i] + nums[left] + nums[i + 2] >= target) break;
102-
while (left < right) {
103-
if (nums[i] + nums[left] + nums[right] < target) {
104-
res += right - left;
105-
left++;
106-
continue;
137+
let ans = 0;
138+
for (let i = 0, n = nums.length; i < n; ++i) {
139+
let j = i + 1;
140+
let k = n - 1;
141+
while (j < k) {
142+
s = nums[i] + nums[j] + nums[k];
143+
if (s >= target) {
144+
--k;
107145
} else {
108-
right--;
109-
continue;
146+
ans += k - j;
147+
++j;
110148
}
111149
}
112150
}
113-
return res;
151+
return ans;
114152
};
115153
```
116154

solution/0200-0299/0259.3Sum Smaller/README_EN.md

+81-43
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,18 @@
5252
```python
5353
class Solution:
5454
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-
6555
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
7067
```
7168

7269
### **Java**
@@ -75,26 +72,71 @@ class Solution:
7572
class Solution {
7673
public int threeSumSmaller(int[] nums, int target) {
7774
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+
}
8288
}
83-
return count;
89+
return ans;
8490
}
91+
}
92+
```
8593

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+
}
94114
}
95115
}
96-
return count;
116+
return ans;
97117
}
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
98140
}
99141
```
100142

@@ -107,26 +149,22 @@ class Solution {
107149
* @return {number}
108150
*/
109151
var threeSumSmaller = function (nums, target) {
110-
let len = nums.length;
111-
if (len < 3) return 0;
112152
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;
123161
} else {
124-
right--;
125-
continue;
162+
ans += k - j;
163+
++j;
126164
}
127165
}
128166
}
129-
return res;
167+
return ans;
130168
};
131169
```
132170

0 commit comments

Comments
 (0)