657 Judge Route Circle
657. Judge Route Circle
1. Question
Input: "UD"
Output: trueInput: "LL"
Output: false2. Implementation
class Solution {
public boolean judgeCircle(String moves) {
int horizontalMove = 0, verticalMove = 0;
for (char move : moves.toCharArray()) {
switch(move) {
case 'U':
--verticalMove;
break;
case 'D':
++verticalMove;
break;
case 'L':
--horizontalMove;
break;
case 'R':
++horizontalMove;
break;
}
}
return horizontalMove == 0 && verticalMove == 0;
}
}3. Time & Space Complexity
Last updated