diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md index 30f9b82ea0797..0cf8d4f95562f 100644 --- a/solution/0600-0699/0624.Maximum Distance in Arrays/README.md +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README.md @@ -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])$。 遍历结束后,即可得到最大距离。 diff --git a/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md b/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md index b6c7a33b09bba..5698f5ae9bf3f 100644 --- a/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md +++ b/solution/0600-0699/0624.Maximum Distance in Arrays/README_EN.md @@ -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. diff --git a/solution/0600-0699/0657.Robot Return to Origin/README.md b/solution/0600-0699/0657.Robot Return to Origin/README.md index 448f42a1a382b..d55967f84ce3a 100644 --- a/solution/0600-0699/0657.Robot Return to Origin/README.md +++ b/solution/0600-0699/0657.Robot Return to Origin/README.md @@ -56,7 +56,20 @@ tags: -### 方法一 +### 方法一:维护坐标 + +我们可以维护一个坐标 $(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)$。 @@ -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 ``` @@ -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; +}; +``` + diff --git a/solution/0600-0699/0657.Robot Return to Origin/README_EN.md b/solution/0600-0699/0657.Robot Return to Origin/README_EN.md index 64ea40b8c8cb6..9b7e6d954c7eb 100644 --- a/solution/0600-0699/0657.Robot Return to Origin/README_EN.md +++ b/solution/0600-0699/0657.Robot Return to Origin/README_EN.md @@ -56,7 +56,20 @@ tags: -### 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)$. @@ -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 ``` @@ -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; +}; +``` + diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.cpp b/solution/0600-0699/0657.Robot Return to Origin/Solution.cpp new file mode 100644 index 0000000000000..c939c0d280c88 --- /dev/null +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.cpp @@ -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; + } +}; diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.go b/solution/0600-0699/0657.Robot Return to Origin/Solution.go new file mode 100644 index 0000000000000..239fb6e9bb691 --- /dev/null +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.go @@ -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 +} diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.java b/solution/0600-0699/0657.Robot Return to Origin/Solution.java index 71d6bd1b40e16..350d3d83ba7d8 100644 --- a/solution/0600-0699/0657.Robot Return to Origin/Solution.java +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.java @@ -1,17 +1,14 @@ 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; } -} \ No newline at end of file +} diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.js b/solution/0600-0699/0657.Robot Return to Origin/Solution.js new file mode 100644 index 0000000000000..32b5a65427ecb --- /dev/null +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.js @@ -0,0 +1,19 @@ +/** + * @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; +}; diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.py b/solution/0600-0699/0657.Robot Return to Origin/Solution.py index f2b5aacd3cbf0..437f58f6dfde0 100644 --- a/solution/0600-0699/0657.Robot Return to Origin/Solution.py +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.py @@ -2,12 +2,13 @@ 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 diff --git a/solution/0600-0699/0657.Robot Return to Origin/Solution.ts b/solution/0600-0699/0657.Robot Return to Origin/Solution.ts index 3cd6874204f84..c02d647362cbb 100644 --- a/solution/0600-0699/0657.Robot Return to Origin/Solution.ts +++ b/solution/0600-0699/0657.Robot Return to Origin/Solution.ts @@ -1,16 +1,15 @@ 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; }