-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1041-RobotBoundedInCircle.cs
32 lines (29 loc) · 1011 Bytes
/
1041-RobotBoundedInCircle.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//-----------------------------------------------------------------------------
// Runtime: 68ms
// Memory Usage: 22 MB
// Link: https://leetcode.com/submissions/detail/375100171/
//-----------------------------------------------------------------------------
namespace LeetCode
{
public class _1041_RobotBoundedInCircle
{
public bool IsRobotBounded(string instructions)
{
var directions = new int[4][] { new int[2] { 0, 1 }, new int[2] { 1, 0 }, new int[2] { 0, -1 }, new int[2] { -1, 0 } };
int x = 0, y = 0, dir = 0;
foreach (var ch in instructions)
{
if (ch == 'L')
dir = (dir + 3) % 4;
else if (ch == 'R')
dir = (dir + 1) % 4;
else
{
x += directions[dir][0];
y += directions[dir][1];
}
}
return (x == 0 && y == 0) || (dir != 0);
}
}
}