657. Judge Route Circle
- Description
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R
(Right), L
(Left), U
(Up) and D
(down). The output should be true or false representing whether the robot makes a circle.
Example 1:
Input: "UD"
Output: true
Example 2:
Input: "LL"
Output: false
- Solution
class Solution {
public boolean judgeCircle(String moves) {
int heng=0;
int zong=0;
char[] out=moves.toCharArray();
for(int i=0;i<out.length;i++){
if(out[i]=='U'){
zong+=1;
}else if(out[i]=='D'){
zong-=1;
}else if(out[i]=='R'){
heng+=1;
}else if(out[i]=='L'){
heng-=1;
}else{
System.out.println("wrong input!");
}
}
return zong==0&&heng==0;
}
}
- 将string转为char数组,之后使用char数组进行判断