mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
657. 机器人能否返回原点 添加Java版本
This commit is contained in:
@ -71,6 +71,21 @@ public:
|
||||
## Java
|
||||
|
||||
```java
|
||||
// 时间复杂度:O(n)
|
||||
// 空间复杂度:如果采用 toCharArray,则是 O(n);如果使用 charAt,则是 O(1)
|
||||
class Solution {
|
||||
public boolean judgeCircle(String moves) {
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
for (char c : moves.toCharArray()) {
|
||||
if (c == 'U') y++;
|
||||
if (c == 'D') y--;
|
||||
if (c == 'L') x++;
|
||||
if (c == 'R') x--;
|
||||
}
|
||||
return x == 0 && y == 0;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Python
|
||||
|
Reference in New Issue
Block a user