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. git切换远程仓库地址

    $ git remote  -vorigin  http://192.168.1.100/aaa/Project.git (fetch)origin  http://192.168.1.100/aaa ...

  2. Windows下安装Python虚拟环境

    Windows下安装Python虚拟环境 虚拟环境安装 需求概要 "虚拟环境"是从电脑独立开辟出来的环境.就好比我们生活中的橱柜中,会把酱油放在一个瓶子里,把醋放在另外一个瓶子里, ...

  3. idea中使用MyBatis Generator

    1.新建maven项目 2.新建Generator配置文件 generator_config.xml <?xml version="1.0" encoding="U ...

  4. 学习MeteoInfo二次开发教程(十二)

    1.添加新的Form窗体: 在解决方案资源管理器中,右键MeteoInfoDemo,“添加”,“Windows 窗体” 2.新窗体中添加好layersLegend1和Layout之后,要把layers ...

  5. Linux SVN 操作详解(转)

    1.将文件checkout到本地目录 svn checkout path(path是服务器上的目录)   例如:svn checkout svn://192.168.1.1/pro/domain    ...

  6. Problem A: Apple(高斯消元)

    可以发现具有非常多的方程, 然后高斯消元就能85分 然而我们发现这些方程组成了一些环, 我们仅仅设出一部分变量即可获得N个方程, 就可以A了 trick 合并方程 #include <cstdi ...

  7. JAVA 8 函数式接口--Consumer

    从JDK8开始java支持函数式编程,JDK也提供了几个常用的函数式接口,这篇主要介绍Consumer接口.文本介绍的顺序依次为: 源码介绍 使用实例 jdk内对Consumer的典型使用 扩展类介绍 ...

  8. 2018-2019-2 20165205 《网络对抗技术》 Exp6 信息收集与漏洞扫描

    2018-2019-2 20165205 <网络对抗技术> Exp6 信息收集与漏洞扫描 实验目标 掌握信息收集的最基本技能与常用工具的方式 实验内容 各种搜索技巧的应用 DNS IP注册 ...

  9. LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [,,] \ / Ou ...

  10. Java序列化相关

    java类实现serializable有什么好处或意义 一个对象序列化的接口,一个类只有实现了Serializable接口,它的对象才是可序列化的.因此如果要序列化某些类的对象,这些类就必须实现Ser ...