[Leetcode]657. Robot Return to Origin

Easy

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.

题目大意:一个机器人根据给定的字符串的指令进行移动(没有移动方向导致的机器人面朝影响,即:如同人朝东南西北移动一样)U是向上移动,D是向下移动,L是向左移动,R是向右移动。如果移动结束后机器人又回到原位则返回true,否则返回false。

可以用一个数组来记录机器人相对于原点的移动,移动结束后如果数组中的数据是0就表示机器人相对原点移动是0,也就是没有移动,返回true,否则返回false。那么这个数组就要有两项,一项用于记录左右移动结果,一项用于记录上下移动结果。代码如下:
class Solution {
public:
    bool judgeCircle(string moves) {
        if(moves.size()==0)return true;
        vector<int> step(2,0);
        for(char c: moves){
            if(c==‘L‘){
                step[0]++;
            }
            else if(c==‘R‘){
                step[0]--;
            }
            else if(c==‘U‘){
                step[1]++;
            }
            else if(c==‘D‘){
                step[1]--;
            }
        }
        if(step[0]==0 && step[1]==0){
            return true;
        }
        else return false;
    }
};

原文地址:https://www.cnblogs.com/cff2121/p/11421053.html

时间: 2024-11-08 16:01:05

[Leetcode]657. Robot Return to Origin的相关文章

【leetcode】657. Robot Return to Origin

Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin/ 1)problem 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,

657. Robot Return to Origin

class Solution: def judgeCircle(self, moves: str) -> bool: u,d,l,r = 0,0,0,0 for move in moves: if move is 'U': u += 1 elif move is 'D': d += 1 elif move is 'L': l += 1 elif move is 'R': r += 1 if u!=d or l!=r: return False return True 76ms,13.1M 原文地

(Easy) Robot Return to Origin LeetCode

Description here 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 mov

leetcode 874 Robot Simulation

leetcode 874 Robot Simulation 限制:32MB, 1s 描述 A robot on an infinite grid starts at point (0, 0) and faces north. The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forw

[LeetCode] Walking Robot Simulation 走路机器人仿真

A robot on an infinite grid starts at point (0, 0) and faces north.? The robot can receive one of three possible types of commands: -2: turn left 90 degrees -1: turn right 90 degrees 1 <= x <= 9: move forward?x?units Some of the grid squares are obs

leetcode 657. Judge Route Circle

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 characte

算法--leetcode 657. Judge Route Circle

题目: 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 char

[LeetCode] 489. Robot Room Cleaner 扫地机器人

Given a robot cleaner in a room modeled as a grid. Each cell in the grid can be empty or blocked. The robot cleaner with 4 given APIs can move forward, turn left or turn right. Each turn it made is 90 degrees. When it tries to move into a blocked cel

【Leetcode周赛】从contest-41开始。(一般是10个contest写一篇文章)

Contest 41 ()(题号) Contest 42 ()(题号) Contest 43 ()(题号) Contest 44 (2018年12月6日,周四上午)(题号653-656) 链接:https://leetcode.com/contest/leetcode-weekly-contest-44 比赛情况记录:就做出来两题,第三题不难,然而就是在算坐标的时候卡住了.orz.结果:2/4,ranking:637/2272.第四题没看题,第三题搞得心情不好了orz. [653]Two Sum