程式語言 - LeetCode - C++ - 657. Robot Return to Origin



題目:


解答:

class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0;
        int y = 0;

        for (char ch : moves) {
            if (ch == 'U') y -= 1;
            else if (ch == 'D') y += 1;
            else if (ch == 'L') x -= 1;
            else if (ch == 'R') x += 1;
        }

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