Skip to content

Commit da48265

Browse files
authored
feat: add solutions to lc problem: No.0657 (#4080)
No.0657.Robot Return to Origin
1 parent 4037c67 commit da48265

File tree

10 files changed

+295
-98
lines changed

10 files changed

+295
-98
lines changed

Diff for: solution/0600-0699/0624.Maximum Distance in Arrays/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ tags:
6464

6565
我们注意到,最大距离一定是两个数组中的一个最大值和另一个最小值之间的距离。因此,我们可以维护两个变量 $\textit{mi}$ 和 $\textit{mx}$,分别表示已经遍历过的数组中的最小值和最大值。初始时 $\textit{mi}$ 和 $\textit{mx}$ 分别为第一个数组的第一个元素和最后一个元素。
6666

67-
接下来,我们从第二个数组开始遍历,对于每个数组,我们首先计算当前数组的第一个元素和 $\textit{mx}$ 之间的距离,以及当前数组的最后一个元素和 $\textit{mi}$ 之间的距离,然后更新最大距离。同时,我们更新 $\textit{mi}$ 和 $\textit{mx}$ 为当前数组的第一个元素和最后一个元素
67+
接下来,我们从第二个数组开始遍历,对于每个数组,我们首先计算当前数组的第一个元素和 $\textit{mx}$ 之间的距离,以及当前数组的最后一个元素和 $\textit{mi}$ 之间的距离,然后更新最大距离。同时,我们更新 $\textit{mi} = \min(\textit{mi}, \textit{arr}[0])$ 和 $\textit{mx} = \max(\textit{mx}, \textit{arr}[\textit{size} - 1])$
6868

6969
遍历结束后,即可得到最大距离。
7070

Diff for: solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ tags:
6161

6262
We notice that the maximum distance must be the distance between the maximum value in one array and the minimum value in another array. Therefore, we can maintain two variables $\textit{mi}$ and $\textit{mx}$, representing the minimum and maximum values of the arrays we have traversed. Initially, $\textit{mi}$ and $\textit{mx}$ are the first and last elements of the first array, respectively.
6363

64-
Next, we traverse from the second array. For each array, we first calculate the distance between the first element of the current array and $\textit{mx}$, and the distance between the last element of the current array and $\textit{mi}$. Then, we update the maximum distance. At the same time, we update $\textit{mi}$ and $\textit{mx}$ to be the first and last elements of the current array.
64+
Next, we traverse from the second array. For each array, we first calculate the distance between the first element of the current array and $\textit{mx}$, and the distance between the last element of the current array and $\textit{mi}$. Then, we update the maximum distance. At the same time, we update $\textit{mi} = \min(\textit{mi}, \textit{arr}[0])$ and $\textit{mx} = \max(\textit{mx}, \textit{arr}[\textit{size} - 1])$.
6565

6666
After traversing all arrays, we get the maximum distance.
6767

Diff for: solution/0600-0699/0657.Robot Return to Origin/README.md

+107-32
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,20 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### 方法一
59+
### 方法一:维护坐标
60+
61+
我们可以维护一个坐标 $(x, y)$,分别表示机器人在水平方向和竖直方向上的移动。
62+
63+
遍历字符串 $\textit{moves}$,根据当前字符的不同,更新坐标 $(x, y)$:
64+
65+
- 如果当前字符是 `'U'`,则 $y$ 加 $1$;
66+
- 如果当前字符是 `'D'`,则 $y$ 减 $1$;
67+
- 如果当前字符是 `'L'`,则 $x$ 减 $1$;
68+
- 如果当前字符是 `'R'`,则 $x$ 加 $1$。
69+
70+
最后,判断 $x$ 和 $y$ 是否都为 $0$ 即可。
71+
72+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{moves}$ 的长度。空间复杂度 $O(1)$。
6073

6174
<!-- tabs:start -->
6275

@@ -67,14 +80,15 @@ class Solution:
6780
def judgeCircle(self, moves: str) -> bool:
6881
x = y = 0
6982
for c in moves:
70-
if c == 'R':
71-
x += 1
72-
elif c == 'L':
73-
x -= 1
74-
elif c == 'U':
75-
y += 1
76-
elif c == 'D':
77-
y -= 1
83+
match c:
84+
case "U":
85+
y += 1
86+
case "D":
87+
y -= 1
88+
case "L":
89+
x -= 1
90+
case "R":
91+
x += 1
7892
return x == 0 and y == 0
7993
```
8094

@@ -84,43 +98,104 @@ class Solution:
8498
class Solution {
8599
public boolean judgeCircle(String moves) {
86100
int x = 0, y = 0;
87-
for (int i = 0; i < moves.length(); ++i) {
88-
char c = moves.charAt(i);
89-
if (c == 'R')
90-
++x;
91-
else if (c == 'L')
92-
--x;
93-
else if (c == 'U')
94-
++y;
95-
else if (c == 'D')
96-
--y;
101+
for (char c : moves.toCharArray()) {
102+
switch (c) {
103+
case 'U' -> y++;
104+
case 'D' -> y--;
105+
case 'L' -> x--;
106+
case 'R' -> x++;
107+
}
97108
}
98109
return x == 0 && y == 0;
99110
}
100111
}
101112
```
102113

114+
#### C++
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
bool judgeCircle(string moves) {
120+
int x = 0, y = 0;
121+
for (char c : moves) {
122+
switch (c) {
123+
case 'U': y++; break;
124+
case 'D': y--; break;
125+
case 'L': x--; break;
126+
case 'R': x++; break;
127+
}
128+
}
129+
return x == 0 && y == 0;
130+
}
131+
};
132+
```
133+
134+
#### Go
135+
136+
```go
137+
func judgeCircle(moves string) bool {
138+
x, y := 0, 0
139+
for _, c := range moves {
140+
switch c {
141+
case 'U':
142+
y++
143+
case 'D':
144+
y--
145+
case 'L':
146+
x--
147+
case 'R':
148+
x++
149+
}
150+
}
151+
return x == 0 && y == 0
152+
}
153+
```
154+
103155
#### TypeScript
104156

105157
```ts
106158
function judgeCircle(moves: string): boolean {
107-
let x = 0,
108-
y = 0;
109-
const dir = {
110-
R: [1, 0],
111-
L: [-1, 0],
112-
U: [0, 1],
113-
D: [0, -1],
114-
};
115-
for (let u of moves) {
116-
const [dx, dy] = dir[u];
117-
x += dx;
118-
y += dy;
159+
let [x, y] = [0, 0];
160+
for (const c of moves) {
161+
if (c === 'U') {
162+
y++;
163+
} else if (c === 'D') {
164+
y--;
165+
} else if (c === 'L') {
166+
x--;
167+
} else {
168+
x++;
169+
}
119170
}
120-
return !x && !y;
171+
return x === 0 && y === 0;
121172
}
122173
```
123174

175+
#### JavaScript
176+
177+
```js
178+
/**
179+
* @param {string} moves
180+
* @return {boolean}
181+
*/
182+
var judgeCircle = function (moves) {
183+
let [x, y] = [0, 0];
184+
for (const c of moves) {
185+
if (c === 'U') {
186+
y++;
187+
} else if (c === 'D') {
188+
y--;
189+
} else if (c === 'L') {
190+
x--;
191+
} else {
192+
x++;
193+
}
194+
}
195+
return x === 0 && y === 0;
196+
};
197+
```
198+
124199
<!-- tabs:end -->
125200

126201
<!-- solution:end -->

Diff for: solution/0600-0699/0657.Robot Return to Origin/README_EN.md

+107-32
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,20 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Maintain Coordinates
60+
61+
We can maintain a coordinate $(x, y)$ to represent the robot's movement in the horizontal and vertical directions.
62+
63+
Traverse the string $\textit{moves}$ and update the coordinate $(x, y)$ based on the current character:
64+
65+
- If the current character is `'U'`, then $y$ increases by $1$;
66+
- If the current character is `'D'$, then $y$ decreases by $1$;
67+
- If the current character is `'L'$, then $x$ decreases by $1$;
68+
- If the current character is `'R'$, then $x$ increases by $1$.
69+
70+
Finally, check if both $x$ and $y$ are $0$.
71+
72+
The time complexity is $O(n)$, where $n$ is the length of the string $\textit{moves}$. The space complexity is $O(1)$.
6073

6174
<!-- tabs:start -->
6275

@@ -67,14 +80,15 @@ class Solution:
6780
def judgeCircle(self, moves: str) -> bool:
6881
x = y = 0
6982
for c in moves:
70-
if c == 'R':
71-
x += 1
72-
elif c == 'L':
73-
x -= 1
74-
elif c == 'U':
75-
y += 1
76-
elif c == 'D':
77-
y -= 1
83+
match c:
84+
case "U":
85+
y += 1
86+
case "D":
87+
y -= 1
88+
case "L":
89+
x -= 1
90+
case "R":
91+
x += 1
7892
return x == 0 and y == 0
7993
```
8094

@@ -84,43 +98,104 @@ class Solution:
8498
class Solution {
8599
public boolean judgeCircle(String moves) {
86100
int x = 0, y = 0;
87-
for (int i = 0; i < moves.length(); ++i) {
88-
char c = moves.charAt(i);
89-
if (c == 'R')
90-
++x;
91-
else if (c == 'L')
92-
--x;
93-
else if (c == 'U')
94-
++y;
95-
else if (c == 'D')
96-
--y;
101+
for (char c : moves.toCharArray()) {
102+
switch (c) {
103+
case 'U' -> y++;
104+
case 'D' -> y--;
105+
case 'L' -> x--;
106+
case 'R' -> x++;
107+
}
97108
}
98109
return x == 0 && y == 0;
99110
}
100111
}
101112
```
102113

114+
#### C++
115+
116+
```cpp
117+
class Solution {
118+
public:
119+
bool judgeCircle(string moves) {
120+
int x = 0, y = 0;
121+
for (char c : moves) {
122+
switch (c) {
123+
case 'U': y++; break;
124+
case 'D': y--; break;
125+
case 'L': x--; break;
126+
case 'R': x++; break;
127+
}
128+
}
129+
return x == 0 && y == 0;
130+
}
131+
};
132+
```
133+
134+
#### Go
135+
136+
```go
137+
func judgeCircle(moves string) bool {
138+
x, y := 0, 0
139+
for _, c := range moves {
140+
switch c {
141+
case 'U':
142+
y++
143+
case 'D':
144+
y--
145+
case 'L':
146+
x--
147+
case 'R':
148+
x++
149+
}
150+
}
151+
return x == 0 && y == 0
152+
}
153+
```
154+
103155
#### TypeScript
104156

105157
```ts
106158
function judgeCircle(moves: string): boolean {
107-
let x = 0,
108-
y = 0;
109-
const dir = {
110-
R: [1, 0],
111-
L: [-1, 0],
112-
U: [0, 1],
113-
D: [0, -1],
114-
};
115-
for (let u of moves) {
116-
const [dx, dy] = dir[u];
117-
x += dx;
118-
y += dy;
159+
let [x, y] = [0, 0];
160+
for (const c of moves) {
161+
if (c === 'U') {
162+
y++;
163+
} else if (c === 'D') {
164+
y--;
165+
} else if (c === 'L') {
166+
x--;
167+
} else {
168+
x++;
169+
}
119170
}
120-
return !x && !y;
171+
return x === 0 && y === 0;
121172
}
122173
```
123174

175+
#### JavaScript
176+
177+
```js
178+
/**
179+
* @param {string} moves
180+
* @return {boolean}
181+
*/
182+
var judgeCircle = function (moves) {
183+
let [x, y] = [0, 0];
184+
for (const c of moves) {
185+
if (c === 'U') {
186+
y++;
187+
} else if (c === 'D') {
188+
y--;
189+
} else if (c === 'L') {
190+
x--;
191+
} else {
192+
x++;
193+
}
194+
}
195+
return x === 0 && y === 0;
196+
};
197+
```
198+
124199
<!-- tabs:end -->
125200

126201
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
bool judgeCircle(string moves) {
4+
int x = 0, y = 0;
5+
for (char c : moves) {
6+
switch (c) {
7+
case 'U': y++; break;
8+
case 'D': y--; break;
9+
case 'L': x--; break;
10+
case 'R': x++; break;
11+
}
12+
}
13+
return x == 0 && y == 0;
14+
}
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
func judgeCircle(moves string) bool {
2+
x, y := 0, 0
3+
for _, c := range moves {
4+
switch c {
5+
case 'U':
6+
y++
7+
case 'D':
8+
y--
9+
case 'L':
10+
x--
11+
case 'R':
12+
x++
13+
}
14+
}
15+
return x == 0 && y == 0
16+
}

0 commit comments

Comments
 (0)