mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 19:44:45 +08:00
657.机器人能否返回原点 添加JavaScript版本
This commit is contained in:
@ -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;
|
||||
};
|
||||
```
|
||||
|
||||
-----------------------
|
||||
|
Reference in New Issue
Block a user