본문 바로가기
Problem Solving/LeetCode

[LeetCode] 657. Robot Return to Origin - Java

by graycode 2026. 9. 18.

• 문제 링크

 

Robot Return to Origin - LeetCode

Can you solve this real interview question? Robot Return to Origin - There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves. You are giv

leetcode.com

 

• 풀이 코드

public class Solution {

    public boolean judgeCircle(String moves) {
        int y = 0, x = 0;
        for (char c : moves.toCharArray()) {
            switch (c) {
                case 'U' -> y++;
                case 'D' -> y--;
                case 'L' -> x--;
                case 'R' -> x++;
                default -> {
                }
            }
        }

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

}

댓글