657. 机器人能否返回原点 添加Java版本

This commit is contained in:
xsduan98
2021-07-30 18:21:18 +08:00
parent 614572b39a
commit 2119e411b7

View File

@ -71,6 +71,21 @@ public:
## Java ## Java
```java ```java
// 时间复杂度O(n)
// 空间复杂度:如果采用 toCharArray则是 On;如果使用 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 ## Python