LeetCode657. Robot Return to Origin

文章目录

一、题目

There is a robot starting at the 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.

You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are ‘R’ (right), ‘L’ (left), ‘U’ (up), and ‘D’ (down).

Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.

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: moves = “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: moves = “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.

Constraints:

1 <= moves.length <= 2 * 104
moves only contains the characters ‘U’, ‘D’, ‘L’ and ‘R’.

二、题解

class Solution {
   
public:
    bool judgeCircle(string moves) {
   
        vector<int> location(2,0);
        for(auto c:moves){
   
            if(c == 'R') location[1]++;
            else if(c == 'L') location[1]--;
            else if(c == 'U') location[0]--;
            else location[0]++;
        }
        return !location[0] && !location[1];
    }
};

相关推荐

  1. LeetCode657. Robot Return to Origin

    2023-12-30 21:14:06       33 阅读
  2. Leetcode-657. 机器人能否返回原点

    2023-12-30 21:14:06       31 阅读
  3. LeetCode657.机器人能否返回原点

    2023-12-30 21:14:06       10 阅读
  4. LeetCode607. 销售员

    2023-12-30 21:14:06       45 阅读
  5. 代码随想录算法训练营29期Day20|LeetCode 654,617,700,98

    2023-12-30 21:14:06       32 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-30 21:14:06       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-30 21:14:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-30 21:14:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-30 21:14:06       18 阅读

热门阅读

  1. mysql 数据查重与查重分页

    2023-12-30 21:14:06       33 阅读
  2. leetcode541. 反转字符串II

    2023-12-30 21:14:06       37 阅读
  3. react入门笔记

    2023-12-30 21:14:06       30 阅读
  4. KSO-SAP ABAP 创建webservice服务,并用soapui测试

    2023-12-30 21:14:06       33 阅读
  5. vue 页面刷新、重置、更新页面所有数据

    2023-12-30 21:14:06       52 阅读
  6. 算法训练营Day25

    2023-12-30 21:14:06       41 阅读
  7. OpenCV-Python(22):直方图均衡化

    2023-12-30 21:14:06       39 阅读
  8. Python实现进度条

    2023-12-30 21:14:06       39 阅读
  9. ARM12.26

    ARM12.26

    2023-12-30 21:14:06      33 阅读
  10. 项目中cesium使用方法

    2023-12-30 21:14:06       33 阅读
  11. 四、KMDF开发之traceview跟踪打印信息

    2023-12-30 21:14:06       37 阅读
  12. 【Yii2】数据库查询方法总结

    2023-12-30 21:14:06       40 阅读