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 ...
随机推荐
- MSXML2.ServerXMLHTTP responseText 获取的内容不完整,解决方案
今天无意发现一个问题, 有个别几个网页使用 MSXML2.ServerXMLHTTP 获取网页源代码的时候,.responseText 总是返回一部分内容,无法获取完整的内容. 经过搜索,找到解决方案 ...
- haploview出现“results file must contain a snp column”的解决方法
将plink文件用“--recode HV ”的参数生成即可 /software/plink --file yourfile --recode HV --snps-only just-acgt --o ...
- QSS网址
http://blog.csdn.net/liang19890820/article/details/51691212 https://www.cnblogs.com/cy568searchx/p/3 ...
- C# string.format用法详解
String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 Object 实例的值的文本等效项. Str ...
- Linux虚拟内存(swap)调优篇-“swappiness”,“vm.dirty_background_ratio”和“vm.dirty_ratio”
Linux虚拟内存(swap)调优篇-“swappiness”,“vm.dirty_background_ratio”和“vm.dirty_ratio” 作者:尹正杰 版权声明:原创作品,谢绝转载 ...
- Matlab2017A破解版安装详细图文教程(附破解补丁) 64位
摘录网址:http://www.jb51.net/softjc/543170.html MATLAB2017a安装教程: 1.下载并解压本站提供的MATLAB2017a破解版安装包,载入右键解压或者使 ...
- MyBatis-Cache
一.一级缓存 /** * 一级缓存(本地缓存):SqlSession 级别.一级缓存是默认开启的,为 SqlSession 级别的一个Map * 与数据库同一次会话期间查询到的数据会放在本地缓存中,以 ...
- Mysql数据库进阶之(分表分库,主从分离)
前言:数据库的优化是一个程序员的分水岭,作为小白我也得去提前学习这方面的数据的 (一) 三范式和逆范式 听起范式这个迟非常专业我来举个简单的栗子: 第一范式就是: 把能够关联的每条数据都拆分成一个 ...
- getResource()和getResourceAsStream以及路径问题【转】【补】
一 getResource 用JAVA获取文件,听似简单,但对于很多像我这样的新人来说,还是掌握颇浅,用起来感觉颇深,大常最经常用的,就是用JAVA的File类,如要取得c:/test.txt文件,就 ...
- Cannot make a static reference to the non-static
public class SeckillServiceImpl implements SeckillService{ private SeckillDao seckillDao; private Su ...