题目描述

初始位置 (0, 0) 处有一个机器人。给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置。

移动顺序由一个字符串表示。每一个动作都是由一个字符来表示的。机器人有效的动作有 R(右),L(左),U(上)和 D(下)。输出应为 true 或 false,表示机器人移动路线是否成圈。

示例 1:

输入: "UD"
输出: true

示例 2:

输入: "LL"
输出: false

思路

设置初始点的坐标为x=0,y=0,从坐标系的角度来看,U就是y+1,D就是y-1,L就是x-1,R就是x+1,因此按照字符串中的指示走,最后判断x和y的值是否等于(0,0)即可。

代码实现

package String;

/**
* 657. Judge Route Circle(判断路线成圈)
* 初始位置 (0, 0) 处有一个机器人。给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置。
* 移动顺序由一个字符串表示。每一个动作都是由一个字符来表示的。机器人有效的动作有 R(右),L(左),U(上)和 D(下)。输出应为 true 或 false,表示机器人移动路线是否成圈。
*/
public class Solution657 {
public static void main(String[] args) {
Solution657 solution657 = new Solution657();
String moves = "LL";
System.out.println(solution657.judgeCircle(moves));
} public boolean judgeCircle(String moves) {
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
switch (c) {
case 'U':
y++;
break;
case 'D':
y--;
break;
case 'R':
x++;
break;
case 'L':
x--;
break;
}
}
return x == 0 && y == 0;
}
}

Leetcode#657. Judge Route Circle(判断路线成圈)的更多相关文章

  1. 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 ...

  2. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  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. [LeetCode] Judge Route Circle 判断路线绕圈

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

  5. Judge Route Circle --判断圆路线

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

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

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

  7. 657. Judge Route Circle

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

  8. LeetCode Judge Route Circle

    原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...

  9. Judge Route Circle

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

随机推荐

  1. IT项目管理分享7个开源项目管理工具

    在一项调查中,有 71% 的组织表示他们在开发过程中会用到敏捷方法. 此外,用敏捷方法管理项目比传统方法管理项目成功率高 28%.在这次工具推荐中,我们从一些比较受欢迎的开源项目管理工具中摘取了支持敏 ...

  2. 如何把dedecms数据生成json

    最近搞dede搞得头大,长话短说! 名称:json数据调用 功能:调用某个远程连接的json接口,方便同远程站点之间进行通信来调取内容 语法: {dede:json url='http://news/ ...

  3. docker 基础之网络管理

    docker网络基础. docker使用到的与linux网络有关的主要技术 Network Namespace(网络命名空间) Veth设备对 Iptables/NetFilter 网桥 路由 标准的 ...

  4. mac 安装pip

    mac 安装pip报错 bogon:~ root# sudo easy_install pip Searching for pip Reading https://pypi.python.org/si ...

  5. JAVA-序列化深拷贝对象

    序列化拷贝方法 @SuppressWarnings("unchecked") public static <T extends Serializable> T clon ...

  6. ssh框架里拦截器的权限验证基本思路【转】

    相关表 序号 表性质 表名 字段 字段 字段 字段 字段 1 基表 用户表 id 帐号 密码     2 基表 角色表 id 角色名       3 基表 权限表 id 权限名 请求路径     4 ...

  7. golang json序列化

    结构体序列化 func main() { var j = js{ Name: "zhangsan", Age: 16, Sal: 1500.3, Intro: "aiha ...

  8. @GetMapping(value="/") , "/" 可加可不加 ,是不是一样的

    @GetMapping(value = "/user") 和  @GetMapping(value = "user") 的区别 1.带上 "/&quo ...

  9. 【leetcode-125】 验证回文串

    给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, a c ...

  10. ACM-ICPC 2018 南京赛区网络预赛 E AC Challenge(状压dp)

    https://nanti.jisuanke.com/t/30994 题意 给你n个题目,对于每个题目,在做这个题目之前,规定了必须先做哪几个题目,第t个做的题目i得分是t×ai+bi问最终的最大得分 ...