leetcode657. 判断路线成圈

题目地址

https://leetcode.com/problems/judge-route-circle

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function judgeCircle(string $moves) :bool {
$length = strlen($moves);
$x = $y = 0;

for ($i = 0; $i < $length; $i++) {
if ($moves[$i] == 'U') {
$y++;
} elseif ($moves[$i] == 'D') {
$y--;
} elseif ($moves[$i] == 'R') {
$x++;
} elseif ($moves[$i] == 'L') {
$x--;
} else {
continue;
}
}

return $x == 0 && $y == 0;
}