From 2119e411b73d818baaec8ca8140c7b619358bd54 Mon Sep 17 00:00:00 2001 From: xsduan98 Date: Fri, 30 Jul 2021 18:21:18 +0800 Subject: [PATCH] =?UTF-8?q?657.=20=E6=9C=BA=E5=99=A8=E4=BA=BA=E8=83=BD?= =?UTF-8?q?=E5=90=A6=E8=BF=94=E5=9B=9E=E5=8E=9F=E7=82=B9=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0Java=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0657.机器人能否返回原点.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/problems/0657.机器人能否返回原点.md b/problems/0657.机器人能否返回原点.md index cd26836c..b1fb6f21 100644 --- a/problems/0657.机器人能否返回原点.md +++ b/problems/0657.机器人能否返回原点.md @@ -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