题目要求

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.

题目分析及思路

题目给出一个机器人的移动序列,若能返回原点则返回true,否则返回false。可以设定水平和垂直两个方向上的计数,若移动之后仍保持方位的不变,则返回到了原点。

python代码​

class Solution:

def judgeCircle(self, moves):

"""

:type moves: str

:rtype: bool

"""

vertical = 0

horizontal = 0

for move in moves:

if move == 'R':

horizontal+=1

elif move == 'L':

horizontal-=1

elif move == 'U':

vertical+=1

else:

vertical-=1

return horizontal == 0 and vertical == 0

最新文章

  1. 读<<领域驱动设计-软件核心复杂性应对之道>>有感
  2. 【结果很简单,过程很艰辛】记阿里云Ons消息队列服务.NET接口填坑过程
  3. HTTP消息结构
  4. POJ3107Godfather[树形DP 树的重心]
  5. iOS开发--UIDatePicker
  6. 基于opencv 的图片模糊判断代码
  7. IOS 学习笔记 2015-03-18
  8. JAVAAPI学习之Calendar类;Calendar类set()、add()、roll()方法区别
  9. iOS - Mac 常用快捷键
  10. Django_创建项目
  11. SLAM数据集整理
  12. selenium3 文件系列之------ opencsv读取csv文件
  13. hdu 5724 Chess 博弈sg+状态压缩
  14. [转]深入理解mysqldump原理
  15. Luogu4717 【模板】快速沃尔什变换(FWT)
  16. UVA 10120 - Gift?!(搜索+规律)
  17. TensorFlow入门(三)多层 CNNs 实现 mnist分类
  18. ElasticSearch(八):springboot集成ElasticSearch集群并使用
  19. Bloom Filter(布隆过滤器)的概念和原理
  20. Map / HashMap 获取Key值的方法

热门文章

  1. FiDDLER教程
  2. 【iCore1S 双核心板_ARM】例程十三:SDIO实验——读取SD卡信息
  3. [备份]EntityFramework
  4. 关于Unity中NGUI的Checkbox复选框、Slider滑动条和Button的6种触发回调事件的方式
  5. Go指南练习_rot13Reader
  6. Scala学习笔记(一):入门
  7. SharePreferences
  8. 从vboot来看:virtualbox 和 vmware 虚拟化软件环境的兼容性(支持能力)的差距真是挺大的!
  9. redis mongodb mysql 三大数据库的更简单的批量操作。批量任务自动聚合器。
  10. 延续(continuation)