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 i

Python3

  1 class Solution:
2 def judgeCircle(self, moves):
3 """
4 :type moves: str
5 :rtype: bool
6 """
7 x = 0
8 y = 0
9 for s in moves:
10 if s == 'U':
11 y += 1
12 elif s == 'D':
13 y -= 1
14 elif s == 'R':
15 x += 1
16 elif s == 'L':
17 x -= 1
18 if (x, y) == (0, 0):
19 return True
20 else:
21 return False

Robot Return to Origin的更多相关文章

  1. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  2. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  3. LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)

    977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...

  4. [Swift]LeetCode657. 机器人能否返回原点 | Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  5. LeetCode 657. Robot Return to Origin

    There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...

  6. 657. Robot Return to Origin

    Description There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequenc ...

  7. LeetCode 657 Robot Return to Origin 解题报告

    题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...

  8. LeetCode 657. Robot Return to Origin (C++)

    题目: There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its ...

  9. C#LeetCode刷题之#657-机器人能否返回原点(Robot Return to Origin)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3959 访问. 在二维平面上,有一个机器人从原点 (0, 0) 开 ...

随机推荐

  1. 从零开始编译Poco C++和VS2015环境配置

    Poco C++是一个C++的开源框架,详细介绍可以问百度介绍更详细,这里我就记录下我从零编译poco-1.9.0和配置 VS2015的环境,以备不时之需. 步骤:一.安装cmake: 二.编译poc ...

  2. ubuntu16.04上vue环境搭建

    $ sudo apt-get install python-software-properties $ curl -sL https://deb.nodesource.com/setup_8.x | ...

  3. note10 元组

    元组 Tuple +元组即不可变(immutable)列表 除了可改变列表内容的方法外,其他方法均适用于元组 因此,索引.切片.len().print等均可用 但是,appeng.extend.del ...

  4. 【转】Python 之 元类

    原文链接: https://stackoverflow.com/questions/100003/what-is-a-metaclass-in-python      http://python.jo ...

  5. java 多线程执行时长统计

    ExecutorService——shutdown方法和awaitTermination方法 shutdown方法:平滑的关闭ExecutorService,当此方法被调用时,ExecutorServ ...

  6. React开发环境搭建(react,babel,webpack webpack-dev-server)

    最终效果: 配置webpack 自动编译脚本, 内容更改后浏览器页面自动刷新,提高效率. 主要靠webpack 的watch 功能. npm 全局安装的包: webpack webpack-cli w ...

  7. 机器学习笔记之三-yolov3+win7+vs2017+gpu+opencv编译

    1.环境安装 1.1 vs2017+cuda9.1+cudnn7.0可以和tensorflow一起安装网上教程多,不多说.       唯一需要注意的是vs2017要安装好2015版本的工具集v140 ...

  8. OkHttp之Interceptor

    先看RealCall 发送一个请求,我们会先创建一个request,然后使用okHttpClient.newCall(request),创建一个call进行网络请求,这个call,就是RealCall ...

  9. 在VS2015中用C++编写可被C#调用的DLL

    VS2015用C++创建动态库DLL步骤如下: (1)启动VS2015-->文件-->新建-->项目,按图二进行选择,选择Win32项目,弹出创建窗口,如第二张图.注意.net版本根 ...

  10. gogs windows

    首先安装 git,然后下载 gogs. 在gogs 文件夹位置 启动. gogs.exe web 打开浏览器,输入 127.0.0.1:3000 ,安装 gogs,注意数据库选择,仓库根目录,管理员帐 ...