在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。
移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R
(右),L
(左),U
(上)和 D
(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
示例 1:
输入: "UD" 输出: true 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。
示例 2:
输入: "LL" 输出: false 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回
无敌水题。其实也可以设置4个变量上下左右的,这样思路更清晰,左==右 && 上==下 ,不过2个变量来说,代码比较简洁
class Solution {
public boolean judgeCircle(String moves) {
int H=0,V=0;//v垂直,h水平
for(int i=0;i<moves.length();i++){
if(moves.charAt(i)=='L')
H++;
else if(moves.charAt(i)=='R')
H--;
else if(moves.charAt(i)=='U')
V++;
else if(moves.charAt(i)=='D')
V--;
}
return H==0 && V==0;
}
}
class Solution {
public boolean judgeCircle(String moves) {
int left=0,right=0,up=0,down=0;
for(int i=0;i<moves.length();i++){
if(moves.charAt(i)=='L')
left++;
else if(moves.charAt(i)=='R')
right++;
else if(moves.charAt(i)=='U')
up++;
else if(moves.charAt(i)=='D')
down++;
}
return left==right&&up==down;
}
}
感觉是网络的原因,下面的beat 100%,看不出优化的差距
class Solution {
public boolean judgeCircle(String moves) {
int RCount = count(moves, 'R');
int LCount = count(moves, 'L');
int UCount = count(moves, 'U');
int DCount = count(moves, 'D');
return RCount == LCount && UCount == DCount;
}
private static int count(String s, char c) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == c) {
count++;
}
}
return count;
}
}