check each step and calculate to see if the destination (x,y) is the same as start


  • 0
    K
    	public boolean judgeCircle(String moves) {
    		int x = 0;
    		int y = 0;
    		char[] steps = moves.toCharArray();
    		for (char c : steps) {
    			switch (c) {
    			case 'U': {
    				y++;
    				break;
    			}
    			case 'D': {
    				y--;
    				break;
    			}
    			case 'R': {
    				x++;
    				break;
    			}
    			case 'L': {
    				x--;
    				break;
    			}
    			}
    		}
    		return (x == 0 && y == 0);
    	}
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.