classSolution { public: booljudgeCircle(string moves){ int x = 0, y = 0; for (constauto& move: moves) { if (move == 'U') { y--; } elseif (move == 'D') { y++; } elseif (move == 'L') { x--; } elseif (move == 'R') { x++; } } return x == 0 && y == 0; } };
[sol1-Python]
1 2 3 4 5 6 7 8 9 10
classSolution(object): defjudgeCircle(self, moves): x = y = 0 for move in moves: if move == 'U': y -= 1 elif move == 'D': y += 1 elif move == 'L': x -= 1 elif move == 'R': x += 1
return x == y == 0
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
booljudgeCircle(char* moves) { int n = strlen(moves), x = 0, y = 0; for (int i = 0; i < n; i++) { if (moves[i] == 'U') { y--; } elseif (moves[i] == 'D') { y++; } elseif (moves[i] == 'L') { x--; } elseif (moves[i] == 'R') { x++; } } return x == 0 && y == 0; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcjudgeCircle(moves string)bool { x, y := 0, 0 length := len(moves) for i := 0; i < length; i++ { switch moves[i] { case'U': y-- case'D': y++ case'L': x-- case'R': x++ } } return x == 0 && y == 0 }