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

This commit is contained in:
posper
2021-07-31 10:28:14 +08:00
parent 4764d993b2
commit 0e832d5183

View File

@ -91,6 +91,8 @@ class Solution {
## Python
```python
# 时间复杂度O(n)
# 空间复杂度O(1)
class Solution:
def judgeCircle(self, moves: str) -> bool:
x = 0 # 记录当前位置
@ -115,6 +117,19 @@ class Solution:
## JavaScript
```js
// 时间复杂度O(n)
// 空间复杂度O(1)
var judgeCircle = function(moves) {
var x = 0; // 记录当前位置
var y = 0;
for (var i = 0; i < moves.length; i++) {
if (moves[i] == 'U') y++;
if (moves[i] == 'D') y--;
if (moves[i] == 'L') x++;
if (moves[i] == 'R') x--;
}
return x == 0 && y == 0;
};
```
-----------------------