diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index ba682a33..805a81e6 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -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; +}; ``` -----------------------