【LeetCode】657. Judge Route Circle

标签(空格分隔): LeetCode


题目地址:https://leetcode.com/problems/judge-route-circle/description/

题目描述:

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 character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:
Input: “UD”
Output: true
Example 2:
Input: “LL”
Output: false

Ways

python里面表达坐标比较简单,直接用列表就可以。

这个题就是判断移动了几次之后是否在原来的位置,只要每步都去修改一次即可。

方法一,O(n):

class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
position = [0, 0]
for move in moves:
if move == 'R':
position[0] += 1
if move == 'L':
position[0] -= 1
if move == 'U':
position[1] += 1
if move == 'D':
position[1] -= 1
return position == [0, 0]

方法二,数左右移动的次数和上下移动的次数是否相等:

class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
return moves.count('L') == moves.count('R') and moves.count('U') == moves.count('D')

方法三,看了Discuss才知道,python原来也可以用复数!

class Solution:
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
directs = {'L':-1, 'R':1, 'U':1j, 'D':-1j}
return 0 == sum(directs[move] for move in moves)

Date

2018 年 1 月 13 日

【LeetCode】657. Judge Route Circle 解题报告的更多相关文章

  1. Leetcode#657. Judge Route Circle(判断路线成圈)

    题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...

  2. LeetCode - 657. Judge Route Circle

    Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...

  3. 657. Judge Route Circle【easy】

    657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...

  4. 657. Judge Route Circle机器人能否返回

    [抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...

  5. 657. Judge Route Circle

    static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...

  6. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  7. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  8. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

  9. 【LeetCode】911. Online Election 解题报告(Python)

    [LeetCode]911. Online Election 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

随机推荐

  1. 你不知道的iostat

    1.       作用 iostat是I/O statistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视.它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况 ...

  2. jQuery添加html绑定事件

    jQuery添加html绑定事件 $("#xxx").on("click",".dev",function(){ });

  3. 60-Lowest Common Ancestor of a Binary Search Tree

    Lowest Common Ancestor of a Binary Search Tree My Submissions QuestionEditorial Solution Total Accep ...

  4. 【leetcode】563. Binary Tree Tilt

    Given the root of a binary tree, return the sum of every tree node's tilt. The tilt of a tree node i ...

  5. 容器之分类与各种测试(四)——multimap

    multiset和multimap的具体区别在于,前者的key值就是自己存储的value,后者的key与value是分开的不相关的. 例程 #include<stdexcept> #inc ...

  6. 100个Shell脚本——【脚本1】打印形状

    [脚本1]打印形状 一.脚本 打印等腰三角形.直角三角形.倒直角三角形.菱形 #!/bin/bash #等腰三角形 read -p "Please input the length:&quo ...

  7. Oracle异常处理——ORA-01502:索引或这类索引的分区处于不可用状态

    Oracle异常处理--ORA-01502:索引或这类索引的分区处于不可用状态参考自:https://www.cnblogs.com/lijiaman/p/9277149.html 1.原因分析经过查 ...

  8. Oracle中创建DB LINK

    当用户要跨本地数据库,访问另外一个数据库表中的数据时,本地数据库中必须创建了远程数据库的dblink,通过dblink本地数据库可以像访问本地数据库一样访问远程数据库表中的数据.下面讲介绍如何在本地数 ...

  9. RHEL 6.5 安装ORACEL11gR2

    1.关闭selinux,用vi /etc/selinux/config selinux=disabled 2.使用yum安装rpm yum -y install compat-db compat-db ...

  10. “==” 和 equals()的区别

    ※ "==" 和 equals()的区别 ※ == :比较. 基本数据类型比较的是值:. 引用类型比较的是地址值. ※ equals(Object o):1)不能比较基本数据类型, ...