There is a robot starting at 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.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Example 1:
Input: "UD" Output: true Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Example 2:
Input: "LL" Output: false Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。
移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R
(右),L
(左),U
(上)和 D
(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
示例 1:
输入: "UD" 输出: true 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。
示例 2:
输入: "LL" 输出: false 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。
思路一:对字符串moves进行遍历,统计U D L R字符出现的个数,如果U = D,L = R,则返回true,否则返回false
对此精简,仅需设置两个变量countL ,countD 当 D出现时countD++,当U出现时countD--。最后计算是否等于0即可。
C++代码实现:
class Solution {
public:
bool judgeCircle(string moves) {
int countL = 0,countU = 0;
for(int i = 0; i < moves.length(); i++)
{
if(moves[i] == 'D')
countU--;
else if(moves[i] == 'U')
countU++;
else if(moves[i] == 'L')
countL++;
else
countL--;
}
if(countL == 0 && countU == 0)
return true;
else
return false;
}
};
static const int _ = []() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
return 0;
}();
JAVA代码实现:
继续做了简化,如果moves字符串的长度不是偶数,则直接return false;
class Solution {
public boolean judgeCircle(String moves) {
if((moves.length() >> 1) << 1 != moves.length() )
return false;
char []ch = moves.toCharArray();
int []arr = new int[126];
for(char st : ch)
{
arr[st]++;
}
if(arr['U'] == arr['D'] && arr['L'] == arr['R'])
return true;
else
return false;
}
}