LeetCode Judge Route Circle
原题链接在这里: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
题解:
判断'U'和'D' && 'R'和 'L'的个数是否相同.
Time Complexity: O(moves.length()). Space: O(1).
AC Java:
class Solution {
public boolean judgeCircle(String moves) {
int x = 0;
int y = 0;
for(int i = 0; i<moves.length(); i++){
char c = moves.charAt(i);
if(c == 'U'){
y++;
}else if(c == 'D'){
y--;
}else if(c == 'R'){
x++;
}else if(c == 'L'){
x--;
}
}
return x==0 && y==0;
}
}
LeetCode Judge Route Circle的更多相关文章
- [LeetCode] 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- ...
- Leetcode#657. Judge Route Circle(判断路线成圈)
题目描述 初始位置 (0, 0) 处有一个机器人.给出它的一系列动作,判断这个机器人的移动路线是否形成一个圆圈,换言之就是判断它是否会移回到原来的位置. 移动顺序由一个字符串表示.每一个动作都是由一个 ...
- 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 - 657. 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 ...
- 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 ...
随机推荐
- dockfile
dockerfile是对镜像的描述 新建一个dockfile文件 docker inspect
- Ajax+Spring MVC实现跨域请求(JSONP)
背景: AJAX向后台(springmvc)发送请求,报错:已阻止交叉源请求:同源策略不允许读取 http://127.0.0.1:8080/DevInfoWeb/getJsonp 上的远程资源.可 ...
- ODS
一般在带有ODS的系统体系结构中,ODS都设计为如下几个作用: 1.在业务系统和数据仓库之间形成一个隔离层 一般的数据仓库应用系统都具有非常复杂的数据来源,这些数据存放在不同的地理位置.不同的数据库. ...
- Linux Shell基础 环境变量配置文件
source命令:使环境变量配置文件强制生效 source 命令会强制执行脚本中的全部命令,而忽略脚本文件的权限.该命令主要用于让重新配置的环境变量配置文件强制生效.source 命令格式如下: [r ...
- awk的输出格式控制:print 和printf
1.两个函数和若干个内部变量控制awk的输出格式: 两个函数:print和printf 内部变量:OFS:输出的列间隔符,默认为tab; ORS:输出的行间隔符,默认为\n printf更加自由化, ...
- 物理分辨率与逻辑分辨率,pt与px
有些小伙伴们,在使用chrome的移动端调试工具调试网页的时候,会发现iphone6上的尺寸为375*667,不禁差异,iphone6的分辨率不是750*1334吗? 实际上调试器上的大小单位不是px ...
- IEnumerable的一些基本方法 补充
接上一篇,我们发现两表连接方式默认为内连接,而我们在SQL中常用到的左连接没有封装方法.换句话说,微软放弃两表左连或右连的这种做法(只有在2个表都存在值时,这样的连接才有意义). 如果要实现表的左连接 ...
- kubernetes 核心对象
Pods Pod是Kubernetes的基本操作单元,也是应用运行的载体.整个Kubernetes系统都是围绕着Pod展开的,比如如何部署运行Pod.如何保证Pod的数量.如何访问Pod等.另外,Po ...
- Python 循环语句(while, for)
# while的使用 # 要注意些循环的时候,要考虑好循环的结束 # 考虑循环结束的方法有2种: # 1.考虑在循环体里改变while 的条件 # 2.在循环体通过break 语句跳出循环 # 方法1 ...
- 10个超有趣的linux命令
本文展示了 10 个有趣的 Linux 动态命令,这些命令和实用功能无关,仅供娱乐!看完此文,你会对 Linux 有个全新的认识,谁说 IT 男就没有屌丝娱乐的一面呢?还等什么,就让我们开始看文章吧~ ...