Skip to content

Commit 5f0f44d

Browse files
authored
feat: update solutions to lc problems: No.2239,2337 (doocs#1478)
* No.2239.Find Closest Number to Zero * No.2337.Move Pieces to Obtain a String
1 parent 128e459 commit 5f0f44d

File tree

11 files changed

+209
-90
lines changed

11 files changed

+209
-90
lines changed

solution/2200-2299/2236.Root Equals Sum of Children/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:直接判断**
47+
48+
我们直接判断根节点的值是否等于左右子节点的值之和即可。
49+
50+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
51+
4652
<!-- tabs:start -->
4753

4854
### **Python3**

solution/2200-2299/2239.Find Closest Number to Zero/README.md

+38-22
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@
4343

4444
<!-- 这里可写通用的实现逻辑 -->
4545

46+
**方法一:一次遍历**
47+
48+
我们定义一个变量 $d$ 来记录当前最小的距离,初始时 $d=\infty$。然后我们遍历数组,对于每个元素 $x$,我们计算 $y=|x|$,如果 $y \lt d$ 或者 $y=d$ 且 $x \gt ans$,我们就更新答案 $ans=x$ 和 $d=y$。
49+
50+
遍历结束后返回答案即可。
51+
52+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$。
53+
4654
<!-- tabs:start -->
4755

4856
### **Python3**
@@ -52,10 +60,10 @@
5260
```python
5361
class Solution:
5462
def findClosestNumber(self, nums: List[int]) -> int:
55-
ans, d = 0, 1000000
56-
for v in nums:
57-
if (t := abs(v)) < d or (t == d and v > ans):
58-
ans, d = v, t
63+
ans, d = 0, inf
64+
for x in nums:
65+
if (y := abs(x)) < d or (y == d and x > ans):
66+
ans, d = x, y
5967
return ans
6068
```
6169

@@ -66,12 +74,12 @@ class Solution:
6674
```java
6775
class Solution {
6876
public int findClosestNumber(int[] nums) {
69-
int ans = 0, d = 1000000;
70-
for (int v : nums) {
71-
int t = Math.abs(v);
72-
if (t < d || (t == d && v > ans)) {
73-
ans = v;
74-
d = t;
77+
int ans = 0, d = 1 << 30;
78+
for (int x : nums) {
79+
int y = Math.abs(x);
80+
if (y < d || (y == d && x > ans)) {
81+
ans = x;
82+
d = y;
7583
}
7684
}
7785
return ans;
@@ -85,12 +93,12 @@ class Solution {
8593
class Solution {
8694
public:
8795
int findClosestNumber(vector<int>& nums) {
88-
int ans = 0, d = 1e6;
89-
for (int& v : nums) {
90-
int t = abs(v);
91-
if (t < d || (t == d && v > ans)) {
92-
ans = v;
93-
d = t;
96+
int ans = 0, d = 1 << 30;
97+
for (int x : nums) {
98+
int y = abs(x);
99+
if (y < d || (y == d && x > ans)) {
100+
ans = x;
101+
d = y;
94102
}
95103
}
96104
return ans;
@@ -102,11 +110,10 @@ public:
102110
103111
```go
104112
func findClosestNumber(nums []int) int {
105-
ans, d := 0, 1000000
106-
for _, v := range nums {
107-
t := abs(v)
108-
if t < d || (t == d && v > ans) {
109-
ans, d = v, t
113+
ans, d := 0, 1<<30
114+
for _, x := range nums {
115+
if y := abs(x); y < d || (y == d && x > ans) {
116+
ans, d = x, y
110117
}
111118
}
112119
return ans
@@ -123,7 +130,16 @@ func abs(x int) int {
123130
### **TypeScript**
124131

125132
```ts
126-
133+
function findClosestNumber(nums: number[]): number {
134+
let [ans, d] = [0, 1 << 30];
135+
for (const x of nums) {
136+
const y = Math.abs(x);
137+
if (y < d || (y == d && x > ans)) {
138+
[ans, d] = [x, y];
139+
}
140+
}
141+
return ans;
142+
}
127143
```
128144

129145
### **...**

solution/2200-2299/2239.Find Closest Number to Zero/README_EN.md

+30-22
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@ Thus, the closest number to 0 in the array is 1.
4545
```python
4646
class Solution:
4747
def findClosestNumber(self, nums: List[int]) -> int:
48-
ans, d = 0, 1000000
49-
for v in nums:
50-
if (t := abs(v)) < d or (t == d and v > ans):
51-
ans, d = v, t
48+
ans, d = 0, inf
49+
for x in nums:
50+
if (y := abs(x)) < d or (y == d and x > ans):
51+
ans, d = x, y
5252
return ans
5353
```
5454

@@ -57,12 +57,12 @@ class Solution:
5757
```java
5858
class Solution {
5959
public int findClosestNumber(int[] nums) {
60-
int ans = 0, d = 1000000;
61-
for (int v : nums) {
62-
int t = Math.abs(v);
63-
if (t < d || (t == d && v > ans)) {
64-
ans = v;
65-
d = t;
60+
int ans = 0, d = 1 << 30;
61+
for (int x : nums) {
62+
int y = Math.abs(x);
63+
if (y < d || (y == d && x > ans)) {
64+
ans = x;
65+
d = y;
6666
}
6767
}
6868
return ans;
@@ -76,12 +76,12 @@ class Solution {
7676
class Solution {
7777
public:
7878
int findClosestNumber(vector<int>& nums) {
79-
int ans = 0, d = 1e6;
80-
for (int& v : nums) {
81-
int t = abs(v);
82-
if (t < d || (t == d && v > ans)) {
83-
ans = v;
84-
d = t;
79+
int ans = 0, d = 1 << 30;
80+
for (int x : nums) {
81+
int y = abs(x);
82+
if (y < d || (y == d && x > ans)) {
83+
ans = x;
84+
d = y;
8585
}
8686
}
8787
return ans;
@@ -93,11 +93,10 @@ public:
9393
9494
```go
9595
func findClosestNumber(nums []int) int {
96-
ans, d := 0, 1000000
97-
for _, v := range nums {
98-
t := abs(v)
99-
if t < d || (t == d && v > ans) {
100-
ans, d = v, t
96+
ans, d := 0, 1<<30
97+
for _, x := range nums {
98+
if y := abs(x); y < d || (y == d && x > ans) {
99+
ans, d = x, y
101100
}
102101
}
103102
return ans
@@ -114,7 +113,16 @@ func abs(x int) int {
114113
### **TypeScript**
115114

116115
```ts
117-
116+
function findClosestNumber(nums: number[]): number {
117+
let [ans, d] = [0, 1 << 30];
118+
for (const x of nums) {
119+
const y = Math.abs(x);
120+
if (y < d || (y == d && x > ans)) {
121+
[ans, d] = [x, y];
122+
}
123+
}
124+
return ans;
125+
}
118126
```
119127

120128
### **...**

solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class Solution {
22
public:
33
int findClosestNumber(vector<int>& nums) {
4-
int ans = 0, d = 1e6;
5-
for (int& v : nums) {
6-
int t = abs(v);
7-
if (t < d || (t == d && v > ans)) {
8-
ans = v;
9-
d = t;
4+
int ans = 0, d = 1 << 30;
5+
for (int x : nums) {
6+
int y = abs(x);
7+
if (y < d || (y == d && x > ans)) {
8+
ans = x;
9+
d = y;
1010
}
1111
}
1212
return ans;

solution/2200-2299/2239.Find Closest Number to Zero/Solution.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
func findClosestNumber(nums []int) int {
2-
ans, d := 0, 1000000
3-
for _, v := range nums {
4-
t := abs(v)
5-
if t < d || (t == d && v > ans) {
6-
ans, d = v, t
2+
ans, d := 0, 1<<30
3+
for _, x := range nums {
4+
if y := abs(x); y < d || (y == d && x > ans) {
5+
ans, d = x, y
76
}
87
}
98
return ans

solution/2200-2299/2239.Find Closest Number to Zero/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
class Solution {
22
public int findClosestNumber(int[] nums) {
3-
int ans = 0, d = 1000000;
4-
for (int v : nums) {
5-
int t = Math.abs(v);
6-
if (t < d || (t == d && v > ans)) {
7-
ans = v;
8-
d = t;
3+
int ans = 0, d = 1 << 30;
4+
for (int x : nums) {
5+
int y = Math.abs(x);
6+
if (y < d || (y == d && x > ans)) {
7+
ans = x;
8+
d = y;
99
}
1010
}
1111
return ans;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Solution:
22
def findClosestNumber(self, nums: List[int]) -> int:
3-
ans, d = 0, 1000000
4-
for v in nums:
5-
if (t := abs(v)) < d or (t == d and v > ans):
6-
ans, d = v, t
3+
ans, d = 0, inf
4+
for x in nums:
5+
if (y := abs(x)) < d or (y == d and x > ans):
6+
ans, d = x, y
77
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function findClosestNumber(nums: number[]): number {
2+
let [ans, d] = [0, 1 << 30];
3+
for (const x of nums) {
4+
const y = Math.abs(x);
5+
if (y < d || (y == d && x > ans)) {
6+
[ans, d] = [x, y];
7+
}
8+
}
9+
return ans;
10+
}

solution/2300-2399/2337.Move Pieces to Obtain a String/README.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060

6161
替换操作实际上是将 `L` 往左移动(`L` 左边为 `_` 时才能移动),`R` 往右移动(`R` 右边是 `_` 时才能移动),但 `L` 无法穿过 `R`。所以,如果去掉 `start``target` 中的所有 `_`,剩下的字符应该是相同的,否则返回 `false`
6262

63-
双指针遍历 `start``target`
63+
我们使用双指针 $i$ 和 $j$ 从头到尾遍历 `start``target`
6464

6565
- 如果当前字符为 `L` 且 $i\lt j$,那么这个 `L` 无法向右移动,返回 `false`
6666
- 如果当前字符为 `R` 且 $i\gt j$,那么这个 `R` 无法向左移动,返回 `false`
@@ -339,6 +339,69 @@ function canChange(start: string, target: string): boolean {
339339
}
340340
```
341341

342+
### **TypeScript**
343+
344+
```ts
345+
function canChange(start: string, target: string): boolean {
346+
if (
347+
[...start].filter(c => c !== '_').join('') !==
348+
[...target].filter(c => c !== '_').join('')
349+
) {
350+
return false;
351+
}
352+
const n = start.length;
353+
let i = 0;
354+
let j = 0;
355+
while (i < n || j < n) {
356+
while (start[i] === '_') {
357+
i++;
358+
}
359+
while (target[j] === '_') {
360+
j++;
361+
}
362+
if (start[i] === 'R') {
363+
if (i > j) {
364+
return false;
365+
}
366+
}
367+
if (start[i] === 'L') {
368+
if (i < j) {
369+
return false;
370+
}
371+
}
372+
i++;
373+
j++;
374+
}
375+
return true;
376+
}
377+
```
378+
379+
```ts
380+
function canChange(start: string, target: string): boolean {
381+
const n = start.length;
382+
let [i, j] = [0, 0];
383+
while (1) {
384+
while (i < n && start[i] === '_') {
385+
++i;
386+
}
387+
while (j < n && target[j] === '_') {
388+
++j;
389+
}
390+
if (i === n && j === n) {
391+
return true;
392+
}
393+
if (i === n || j === n || start[i] !== target[j]) {
394+
return false;
395+
}
396+
if ((start[i] === 'L' && i < j) || (start[i] === 'R' && i > j)) {
397+
return false;
398+
}
399+
++i;
400+
++j;
401+
}
402+
}
403+
```
404+
342405
### **...**
343406

344407
```

solution/2300-2399/2337.Move Pieces to Obtain a String/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,32 @@ function canChange(start: string, target: string): boolean {
317317
}
318318
```
319319

320+
```ts
321+
function canChange(start: string, target: string): boolean {
322+
const n = start.length;
323+
let [i, j] = [0, 0];
324+
while (1) {
325+
while (i < n && start[i] === '_') {
326+
++i;
327+
}
328+
while (j < n && target[j] === '_') {
329+
++j;
330+
}
331+
if (i === n && j === n) {
332+
return true;
333+
}
334+
if (i === n || j === n || start[i] !== target[j]) {
335+
return false;
336+
}
337+
if ((start[i] === 'L' && i < j) || (start[i] === 'R' && i > j)) {
338+
return false;
339+
}
340+
++i;
341+
++j;
342+
}
343+
}
344+
```
345+
320346
### **...**
321347

322348
```

0 commit comments

Comments
 (0)