Skip to content

Commit 0ec4c29

Browse files
authored
feat: update lc problems (#3635)
1 parent 6ae17e2 commit 0ec4c29

File tree

30 files changed

+540
-311
lines changed

30 files changed

+540
-311
lines changed

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

-28
Original file line numberDiff line numberDiff line change
@@ -103,34 +103,6 @@ func addDigits(num int) int {
103103

104104
#### Rust
105105

106-
```rust
107-
impl Solution {
108-
pub fn add_digits(num: i32) -> i32 {
109-
if num < 10 {
110-
return num;
111-
}
112-
Self::add_digits(
113-
num.to_string()
114-
.chars()
115-
.map(|c| c.to_string().parse::<i32>().unwrap())
116-
.sum::<i32>(),
117-
)
118-
}
119-
}
120-
```
121-
122-
<!-- tabs:end -->
123-
124-
<!-- solution:end -->
125-
126-
<!-- solution:start -->
127-
128-
### 方法二
129-
130-
<!-- tabs:start -->
131-
132-
#### Rust
133-
134106
```rust
135107
impl Solution {
136108
pub fn add_digits(mut num: i32) -> i32 {

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

-28
Original file line numberDiff line numberDiff line change
@@ -101,34 +101,6 @@ func addDigits(num int) int {
101101

102102
#### Rust
103103

104-
```rust
105-
impl Solution {
106-
pub fn add_digits(num: i32) -> i32 {
107-
if num < 10 {
108-
return num;
109-
}
110-
Self::add_digits(
111-
num.to_string()
112-
.chars()
113-
.map(|c| c.to_string().parse::<i32>().unwrap())
114-
.sum::<i32>(),
115-
)
116-
}
117-
}
118-
```
119-
120-
<!-- tabs:end -->
121-
122-
<!-- solution:end -->
123-
124-
<!-- solution:start -->
125-
126-
### Solution 2
127-
128-
<!-- tabs:start -->
129-
130-
#### Rust
131-
132104
```rust
133105
impl Solution {
134106
pub fn add_digits(mut num: i32) -> i32 {
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
impl Solution {
2-
pub fn add_digits(num: i32) -> i32 {
3-
if num < 10 {
4-
return num;
5-
}
6-
Self::add_digits(
7-
num.to_string()
8-
.chars()
9-
.map(|c| c.to_string().parse::<i32>().unwrap())
10-
.sum::<i32>(),
11-
)
2+
pub fn add_digits(mut num: i32) -> i32 {
3+
((num - 1) % 9) + 1
124
}
135
}

solution/0200-0299/0258.Add Digits/Solution2.rs

-5
This file was deleted.

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

+69-36
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,18 @@ tags:
6262

6363
<!-- solution:start -->
6464

65-
### 方法一
65+
### 方法一:排序 + 双指针 + 枚举
66+
67+
由于元素的顺序不影响结果,我们可以先对数组进行排序,然后使用双指针的方法来解决这个问题。
68+
69+
我们先将数组排序,然后枚举第一个元素 $\textit{nums}[i]$,并在 $\textit{nums}[i+1:n-1]$ 的区间内使用双指针分别指向 $\textit{nums}[j]$ 和 $\textit{nums}[k]$,其中 $j$ 是 $\textit{nums}[i]$ 的下一个元素,而 $k$ 是数组的最后一个元素。
70+
71+
- 如果 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] < \textit{target}$,那么对于任意 $j \lt k' \leq k$ 的元素,都有 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k'] \lt \textit{target}$,一共有 $k - j$ 个这样的 $k'$,我们将 $k - j$ 累加到答案中。接下来,将 $j$ 右移一个位置,继续寻找下一个满足条件的 $k$,直到 $j \geq k$ 为止。
72+
- 如果 $\textit{nums}[i] + \textit{nums}[j] + \textit{nums}[k] \geq \textit{target}$,那么对于任意 $j \leq j' \lt k$ 的元素,都不可能使得 $\textit{nums}[i] + \textit{nums}[j'] + \textit{nums}[k] \lt \textit{target}$,因此我们将 $k$ 左移一个位置,继续寻找下一个满足条件的 $k$,直到 $j \geq k$ 为止。
73+
74+
枚举完所有的 $i$ 后,我们就得到了满足条件的三元组的个数。
75+
76+
时间复杂度 $O(n^2)$,空间复杂度 $O(\log n)$。其中 $n$ 是数组 $\textit{nums}$ 的长度。
6677

6778
<!-- tabs:start -->
6879

@@ -73,15 +84,15 @@ class Solution:
7384
def threeSumSmaller(self, nums: List[int], target: int) -> int:
7485
nums.sort()
7586
ans, n = 0, len(nums)
76-
for i in range(n):
87+
for i in range(n - 2):
7788
j, k = i + 1, n - 1
7889
while j < k:
79-
s = nums[i] + nums[j] + nums[k]
80-
if s >= target:
81-
k -= 1
82-
else:
90+
x = nums[i] + nums[j] + nums[k]
91+
if x < target:
8392
ans += k - j
8493
j += 1
94+
else:
95+
k -= 1
8596
return ans
8697
```
8798

@@ -91,17 +102,16 @@ class Solution:
91102
class Solution {
92103
public int threeSumSmaller(int[] nums, int target) {
93104
Arrays.sort(nums);
94-
int ans = 0;
95-
for (int i = 0, n = nums.length; i < n; ++i) {
96-
int j = i + 1;
97-
int k = n - 1;
105+
int ans = 0, n = nums.length;
106+
for (int i = 0; i + 2 < n; ++i) {
107+
int j = i + 1, k = n - 1;
98108
while (j < k) {
99-
int s = nums[i] + nums[j] + nums[k];
100-
if (s >= target) {
101-
--k;
102-
} else {
109+
int x = nums[i] + nums[j] + nums[k];
110+
if (x < target) {
103111
ans += k - j;
104112
++j;
113+
} else {
114+
--k;
105115
}
106116
}
107117
}
@@ -116,17 +126,17 @@ class Solution {
116126
class Solution {
117127
public:
118128
int threeSumSmaller(vector<int>& nums, int target) {
119-
sort(nums.begin(), nums.end());
120-
int ans = 0;
121-
for (int i = 0, n = nums.size(); i < n; ++i) {
129+
ranges::sort(nums);
130+
int ans = 0, n = nums.size();
131+
for (int i = 0; i + 2 < n; ++i) {
122132
int j = i + 1, k = n - 1;
123133
while (j < k) {
124-
int s = nums[i] + nums[j] + nums[k];
125-
if (s >= target)
126-
--k;
127-
else {
134+
int x = nums[i] + nums[j] + nums[k];
135+
if (x < target) {
128136
ans += k - j;
129137
++j;
138+
} else {
139+
--k;
130140
}
131141
}
132142
}
@@ -138,22 +148,45 @@ public:
138148
#### Go
139149
140150
```go
141-
func threeSumSmaller(nums []int, target int) int {
151+
func threeSumSmaller(nums []int, target int) (ans int) {
142152
sort.Ints(nums)
143-
ans := 0
144-
for i, n := 0, len(nums); i < n; i++ {
153+
n := len(nums)
154+
for i := 0; i < n-2; i++ {
145155
j, k := i+1, n-1
146156
for j < k {
147-
s := nums[i] + nums[j] + nums[k]
148-
if s >= target {
149-
k--
150-
} else {
157+
x := nums[i] + nums[j] + nums[k]
158+
if x < target {
151159
ans += k - j
152160
j++
161+
} else {
162+
k--
153163
}
154164
}
155165
}
156-
return ans
166+
return
167+
}
168+
```
169+
170+
#### TypeScript
171+
172+
```ts
173+
function threeSumSmaller(nums: number[], target: number): number {
174+
nums.sort((a, b) => a - b);
175+
const n = nums.length;
176+
let ans = 0;
177+
for (let i = 0; i < n - 2; ++i) {
178+
let [j, k] = [i + 1, n - 1];
179+
while (j < k) {
180+
const x = nums[i] + nums[j] + nums[k];
181+
if (x < target) {
182+
ans += k - j;
183+
++j;
184+
} else {
185+
--k;
186+
}
187+
}
188+
}
189+
return ans;
157190
}
158191
```
159192

@@ -167,17 +200,17 @@ func threeSumSmaller(nums []int, target int) int {
167200
*/
168201
var threeSumSmaller = function (nums, target) {
169202
nums.sort((a, b) => a - b);
203+
const n = nums.length;
170204
let ans = 0;
171-
for (let i = 0, n = nums.length; i < n; ++i) {
172-
let j = i + 1;
173-
let k = n - 1;
205+
for (let i = 0; i < n - 2; ++i) {
206+
let [j, k] = [i + 1, n - 1];
174207
while (j < k) {
175-
s = nums[i] + nums[j] + nums[k];
176-
if (s >= target) {
177-
--k;
178-
} else {
208+
const x = nums[i] + nums[j] + nums[k];
209+
if (x < target) {
179210
ans += k - j;
180211
++j;
212+
} else {
213+
--k;
181214
}
182215
}
183216
}

0 commit comments

Comments
 (0)