Skip to content

feat: add solutions to lc problem: No.0657 #4080

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ tags:

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tags:

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.

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.
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])$.

After traversing all arrays, we get the maximum distance.

Expand Down
139 changes: 107 additions & 32 deletions solution/0600-0699/0657.Robot Return to Origin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:维护坐标

我们可以维护一个坐标 $(x, y)$,分别表示机器人在水平方向和竖直方向上的移动。

遍历字符串 $\textit{moves}$,根据当前字符的不同,更新坐标 $(x, y)$:

- 如果当前字符是 `'U'`,则 $y$ 加 $1$;
- 如果当前字符是 `'D'`,则 $y$ 减 $1$;
- 如果当前字符是 `'L'`,则 $x$ 减 $1$;
- 如果当前字符是 `'R'`,则 $x$ 加 $1$。

最后,判断 $x$ 和 $y$ 是否都为 $0$ 即可。

时间复杂度 $O(n)$,其中 $n$ 为字符串 $\textit{moves}$ 的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -67,14 +80,15 @@ class Solution:
def judgeCircle(self, moves: str) -> bool:
x = y = 0
for c in moves:
if c == 'R':
x += 1
elif c == 'L':
x -= 1
elif c == 'U':
y += 1
elif c == 'D':
y -= 1
match c:
case "U":
y += 1
case "D":
y -= 1
case "L":
x -= 1
case "R":
x += 1
return x == 0 and y == 0
```

Expand All @@ -84,43 +98,104 @@ class Solution:
class Solution {
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (int i = 0; i < moves.length(); ++i) {
char c = moves.charAt(i);
if (c == 'R')
++x;
else if (c == 'L')
--x;
else if (c == 'U')
++y;
else if (c == 'D')
--y;
for (char c : moves.toCharArray()) {
switch (c) {
case 'U' -> y++;
case 'D' -> y--;
case 'L' -> x--;
case 'R' -> x++;
}
}
return x == 0 && y == 0;
}
}
```

#### C++

```cpp
class Solution {
public:
bool judgeCircle(string moves) {
int x = 0, y = 0;
for (char c : moves) {
switch (c) {
case 'U': y++; break;
case 'D': y--; break;
case 'L': x--; break;
case 'R': x++; break;
}
}
return x == 0 && y == 0;
}
};
```

#### Go

```go
func judgeCircle(moves string) bool {
x, y := 0, 0
for _, c := range moves {
switch c {
case 'U':
y++
case 'D':
y--
case 'L':
x--
case 'R':
x++
}
}
return x == 0 && y == 0
}
```

#### TypeScript

```ts
function judgeCircle(moves: string): boolean {
let x = 0,
y = 0;
const dir = {
R: [1, 0],
L: [-1, 0],
U: [0, 1],
D: [0, -1],
};
for (let u of moves) {
const [dx, dy] = dir[u];
x += dx;
y += dy;
let [x, y] = [0, 0];
for (const c of moves) {
if (c === 'U') {
y++;
} else if (c === 'D') {
y--;
} else if (c === 'L') {
x--;
} else {
x++;
}
}
return !x && !y;
return x === 0 && y === 0;
}
```

#### JavaScript

```js
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function (moves) {
let [x, y] = [0, 0];
for (const c of moves) {
if (c === 'U') {
y++;
} else if (c === 'D') {
y--;
} else if (c === 'L') {
x--;
} else {
x++;
}
}
return x === 0 && y === 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
139 changes: 107 additions & 32 deletions solution/0600-0699/0657.Robot Return to Origin/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,20 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Maintain Coordinates

We can maintain a coordinate $(x, y)$ to represent the robot's movement in the horizontal and vertical directions.

Traverse the string $\textit{moves}$ and update the coordinate $(x, y)$ based on the current character:

- If the current character is `'U'`, then $y$ increases by $1$;
- If the current character is `'D'$, then $y$ decreases by $1$;
- If the current character is `'L'$, then $x$ decreases by $1$;
- If the current character is `'R'$, then $x$ increases by $1$.

Finally, check if both $x$ and $y$ are $0$.

The time complexity is $O(n)$, where $n$ is the length of the string $\textit{moves}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -67,14 +80,15 @@ class Solution:
def judgeCircle(self, moves: str) -> bool:
x = y = 0
for c in moves:
if c == 'R':
x += 1
elif c == 'L':
x -= 1
elif c == 'U':
y += 1
elif c == 'D':
y -= 1
match c:
case "U":
y += 1
case "D":
y -= 1
case "L":
x -= 1
case "R":
x += 1
return x == 0 and y == 0
```

Expand All @@ -84,43 +98,104 @@ class Solution:
class Solution {
public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (int i = 0; i < moves.length(); ++i) {
char c = moves.charAt(i);
if (c == 'R')
++x;
else if (c == 'L')
--x;
else if (c == 'U')
++y;
else if (c == 'D')
--y;
for (char c : moves.toCharArray()) {
switch (c) {
case 'U' -> y++;
case 'D' -> y--;
case 'L' -> x--;
case 'R' -> x++;
}
}
return x == 0 && y == 0;
}
}
```

#### C++

```cpp
class Solution {
public:
bool judgeCircle(string moves) {
int x = 0, y = 0;
for (char c : moves) {
switch (c) {
case 'U': y++; break;
case 'D': y--; break;
case 'L': x--; break;
case 'R': x++; break;
}
}
return x == 0 && y == 0;
}
};
```

#### Go

```go
func judgeCircle(moves string) bool {
x, y := 0, 0
for _, c := range moves {
switch c {
case 'U':
y++
case 'D':
y--
case 'L':
x--
case 'R':
x++
}
}
return x == 0 && y == 0
}
```

#### TypeScript

```ts
function judgeCircle(moves: string): boolean {
let x = 0,
y = 0;
const dir = {
R: [1, 0],
L: [-1, 0],
U: [0, 1],
D: [0, -1],
};
for (let u of moves) {
const [dx, dy] = dir[u];
x += dx;
y += dy;
let [x, y] = [0, 0];
for (const c of moves) {
if (c === 'U') {
y++;
} else if (c === 'D') {
y--;
} else if (c === 'L') {
x--;
} else {
x++;
}
}
return !x && !y;
return x === 0 && y === 0;
}
```

#### JavaScript

```js
/**
* @param {string} moves
* @return {boolean}
*/
var judgeCircle = function (moves) {
let [x, y] = [0, 0];
for (const c of moves) {
if (c === 'U') {
y++;
} else if (c === 'D') {
y--;
} else if (c === 'L') {
x--;
} else {
x++;
}
}
return x === 0 && y === 0;
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
15 changes: 15 additions & 0 deletions solution/0600-0699/0657.Robot Return to Origin/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
bool judgeCircle(string moves) {
int x = 0, y = 0;
for (char c : moves) {
switch (c) {
case 'U': y++; break;
case 'D': y--; break;
case 'L': x--; break;
case 'R': x++; break;
}
}
return x == 0 && y == 0;
}
};
16 changes: 16 additions & 0 deletions solution/0600-0699/0657.Robot Return to Origin/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
func judgeCircle(moves string) bool {
x, y := 0, 0
for _, c := range moves {
switch c {
case 'U':
y++
case 'D':
y--
case 'L':
x--
case 'R':
x++
}
}
return x == 0 && y == 0
}
Loading