From a48b82f894d9136e3d16527532011258bd23a9c0 Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Fri, 10 Dec 2021 17:28:44 +0800 Subject: [PATCH 1/2] feat: add solutions to lc problem: No.1041 1041.Robot Bounded In Circle --- .../1041.Robot Bounded In Circle/README.md | 31 ++++++++++- .../1041.Robot Bounded In Circle/README_EN.md | 31 ++++++++++- .../Solution.java | 52 +++++-------------- .../1041.Robot Bounded In Circle/Solution.py | 12 +++++ 4 files changed, 82 insertions(+), 44 deletions(-) create mode 100644 solution/1000-1099/1041.Robot Bounded In Circle/Solution.py diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README.md b/solution/1000-1099/1041.Robot Bounded In Circle/README.md index 3b5a4c0dda5e4..178296fe1a32c 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README.md @@ -65,7 +65,18 @@ ```python - +class Solution: + def isRobotBounded(self, instructions: str) -> bool: + dicertion = [0] * 4 + cur = 0 + for i in range(len(instructions)): + if instructions[i] == 'L': + cur = cur + 1 if cur < 3 else 0 + elif instructions[i] == 'R': + cur = cur - 1 if cur > 0 else 3 + else: + dicertion[cur] += 1 + return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) ``` ### **Java** @@ -73,7 +84,23 @@ ```java - +class Solution { + public boolean isRobotBounded(String instructions) { + int[] direction = new int[4]; + int cur = 0; + char[] chars = instructions.toCharArray(); + for (char c : chars) { + if (c == 'L') { + cur = cur < 3 ? cur + 1 : 0; + } else if (c == 'R') { + cur = cur > 0 ? cur - 1 : 3; + } else { + direction[cur] += 1; + } + } + return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]); + } +} ``` ### **...** diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md index 854339838a8c0..41497a1964757 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md @@ -55,13 +55,40 @@ When repeating these instructions, the robot remains in the circle of radius 2 c ### **Python3** ```python - +class Solution: + def isRobotBounded(self, instructions: str) -> bool: + dicertion = [0] * 4 + cur = 0 + for i in range(len(instructions)): + if instructions[i] == 'L': + cur = cur + 1 if cur < 3 else 0 + elif instructions[i] == 'R': + cur = cur - 1 if cur > 0 else 3 + else: + dicertion[cur] += 1 + return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) ``` ### **Java** ```java - +class Solution { + public boolean isRobotBounded(String instructions) { + int[] direction = new int[4]; + int cur = 0; + char[] chars = instructions.toCharArray(); + for (char c : chars) { + if (c == 'L') { + cur = cur < 3 ? cur + 1 : 0; + } else if (c == 'R') { + cur = cur > 0 ? cur - 1 : 3; + } else { + direction[cur] += 1; + } + } + return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]); + } +} ``` ### **...** diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.java b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.java index f32d4947b45b4..b8449dcb2c386 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.java +++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.java @@ -1,45 +1,17 @@ class Solution { public boolean isRobotBounded(String instructions) { - int col = 0; - int row = 0; - char[] orders = instructions.toCharArray(); - int order = 0; - for (int i = 0; i < 4; i++) { - for (char ch : orders) { - if (ch == 'L') { - order--; - if (order == -3) { - order = 1; - } - } else if (ch == 'R') { - order++; - if (order == 2) { - order = -2; - } - } else { - switch (order) { - case 0: - row++; - break; - case 1: - col++; - break; - case -1: - col--; - break; - case -2: - row--; - break; - default: - break; - } - } - } - if (col == 0 && row == 0) { - return true; + int[] direction = new int[4]; + int cur = 0; + char[] chars = instructions.toCharArray(); + for (char c : chars) { + if (c == 'L') { + cur = cur < 3 ? cur + 1 : 0; + } else if (c == 'R') { + cur = cur > 0 ? cur - 1 : 3; + } else { + direction[cur] += 1; } } - - return false; + return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]); } -} +} \ No newline at end of file diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py new file mode 100644 index 0000000000000..b5fdf649b7349 --- /dev/null +++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def isRobotBounded(self, instructions: str) -> bool: + dicertion = [0] * 4 + cur = 0 + for i in range(len(instructions)): + if instructions[i] == 'L': + cur = cur + 1 if cur < 3 else 0 + elif instructions[i] == 'R': + cur = cur - 1 if cur > 0 else 3 + else: + dicertion[cur] += 1 + return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) \ No newline at end of file From 5fc8b7f441ee9e2f7f5e9d9055ffeb70e1a232fd Mon Sep 17 00:00:00 2001 From: hongyiheng Date: Fri, 10 Dec 2021 17:31:43 +0800 Subject: [PATCH 2/2] feat: add solutions to lc problem: No.1041 1041.Robot Bounded In Circle --- solution/1000-1099/1041.Robot Bounded In Circle/README.md | 6 +++--- .../1000-1099/1041.Robot Bounded In Circle/README_EN.md | 6 +++--- solution/1000-1099/1041.Robot Bounded In Circle/Solution.py | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README.md b/solution/1000-1099/1041.Robot Bounded In Circle/README.md index 178296fe1a32c..bbacf6ba0ecd0 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README.md @@ -67,7 +67,7 @@ ```python class Solution: def isRobotBounded(self, instructions: str) -> bool: - dicertion = [0] * 4 + direction = [0] * 4 cur = 0 for i in range(len(instructions)): if instructions[i] == 'L': @@ -75,8 +75,8 @@ class Solution: elif instructions[i] == 'R': cur = cur - 1 if cur > 0 else 3 else: - dicertion[cur] += 1 - return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) + direction[cur] += 1 + return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3]) ``` ### **Java** diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md index 41497a1964757..8af6c0bb382de 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md +++ b/solution/1000-1099/1041.Robot Bounded In Circle/README_EN.md @@ -57,7 +57,7 @@ When repeating these instructions, the robot remains in the circle of radius 2 c ```python class Solution: def isRobotBounded(self, instructions: str) -> bool: - dicertion = [0] * 4 + direction = [0] * 4 cur = 0 for i in range(len(instructions)): if instructions[i] == 'L': @@ -65,8 +65,8 @@ class Solution: elif instructions[i] == 'R': cur = cur - 1 if cur > 0 else 3 else: - dicertion[cur] += 1 - return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) + direction[cur] += 1 + return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3]) ``` ### **Java** diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py index b5fdf649b7349..1ca7aa41caef2 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py +++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.py @@ -1,6 +1,6 @@ class Solution: def isRobotBounded(self, instructions: str) -> bool: - dicertion = [0] * 4 + direction = [0] * 4 cur = 0 for i in range(len(instructions)): if instructions[i] == 'L': @@ -8,5 +8,5 @@ def isRobotBounded(self, instructions: str) -> bool: elif instructions[i] == 'R': cur = cur - 1 if cur > 0 else 3 else: - dicertion[cur] += 1 - return cur != 0 or (dicertion[0] == dicertion[2] and dicertion[1] == dicertion[3]) \ No newline at end of file + direction[cur] += 1 + return cur != 0 or (direction[0] == direction[2] and direction[1] == direction[3]) \ No newline at end of file