Leetcode#657. Judge Route Circle(判断路线成圈)
题目描述
初始位置 (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(判断路线成圈)的更多相关文章
- 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 ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 657. Judge Route Circle【easy】
657. Judge Route Circle[easy] Initially, there is a Robot at position (0, 0). Given a sequence of it ...
- [LeetCode] Judge Route Circle 判断路线绕圈
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- Judge Route Circle --判断圆路线
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
- 657. Judge Route Circle机器人能否返回
[抄题]: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...
- 657. Judge Route Circle
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- LeetCode Judge Route Circle
原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...
- Judge Route Circle
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot m ...
随机推荐
- bs4抓取糗事百科
抓取糗事百科内容及评论,不包含图片信息.user-agent填入浏览器的即可.user-agent对应的value,360极速浏览器的话,可以在地址栏输入about:version,回车,用户代理后面 ...
- Day6--Python--小数据池和再谈编码
一.小数据池 参考 目的:缓存我们字符串,整数,布尔值.在使用的时候不需要创建过多的对象 缓存: int, str, bool int: 缓存范围 -5~256 str: 1. 长度小于等于1,直接缓 ...
- Android app:transformNativeLibsWithStripDebugSymbolForDebug错误分析
升级NDK解决问题: 先清除 Android/Sdk/ndk-bundle/ 下的内容从 https://developer.android.google.cn/ndk/downloads/older ...
- HTML学习笔记Day5
一.CSS属性 1.文本溢出是否“...”显示属性:text-overflow:clip(不显示省略标记)/ellipsis(文本溢出时“...”显示) 定义此属性有四个必要条件:1)须有容器宽度:w ...
- linux c 编程 ------ 通过设备节点调用驱动
驱动程序如下,加载驱动后,会在/dev文件夹下生成一个文件hello_device_node,是此驱动的设备节点 #include <linux/init.h> #include < ...
- python自动化开发-[第七天]-面向对象
今日概要: 1.继承 2.封装 3.多态与多态性 4.反射 5.绑定方法和非绑定方法 一.新式类和经典类的区别 大前提: 1.只有在python2中才分新式类和经典类,python3中统一都是新式类 ...
- SQL知识以及SQL语句简单实践
综述 大家都知道SQL是结构化查询语言,是关系数据库的标准语言,是一个综合的,功能极强的同时又简洁易学的,它集级数据查询(Data Quest),数据操纵(Data Manipulation),数据定 ...
- JDBC-DbUtils
依赖 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=" ...
- 1、JPA-HelloWorld
/** * JPA 是 hibernate 的一个抽象(就像JDBC和JDBC驱动的关系) * JPA 本质上是一种 ORM 规范,不是 ORM 框架,因为 JPA 并未提供 ORM 实现,只是制订了 ...
- Web API中的返回值类型
WebApi中的返回值类型大致可分为四种: Void/ IHttpActionResult/ HttpResponseMessage /自定义类型 一.Void void申明方法没有返回值,执行成功后 ...