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 ...
随机推荐
- java CountDownLatch 控制异步和同步
应用场景举例: 执行A项目的方法,需要调用B项目.C项目.D项目的接口方法. 需求: 异步调用B.C.D项目的接口方法,且每个接口都调用结束后,A项目的方法才可以结束. 注:如果需要获取接口返回结果, ...
- [原创]spring及springmvc精简版--IOC
本篇博客为自己学习spring和springmvc的一个总结.主要以代码为主,至于文字性描述理解性东西,可以自行百度.有认识不妥的地方,还望指出,相互学习. 以前很困惑spring中的一些概念,在学习 ...
- [转载]OpenWRT使用wifidog实现强制认证的WIFI热点 | 半个橙子
首先安装wifidog到OpenWRT的路由器: opkg update opkg install wifidog wifidog依赖下面这些模块: iptables-mod-extra iptabl ...
- Luogu-1527 [国家集训队]矩阵乘法
Luogu-1527 [国家集训队]矩阵乘法 题面 Luogu-1527 题解 昨天学CDQ分治时做了一些题,但是因为题(wo)太(tai)水(lan)了(le)并没有整理 学了一晚上的整体二分,拿这 ...
- C#反射第一天
[转]C#反射 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等. ...
- MapReduce-输入分片与记录
一个输入分片(split)就是一个由单个map操作来处理的输入块.每一个map操作只处理一个输入分片.每个分片被划分为若干个记录,每条记录就是一个键值对,map一个接一个地处理记录.输入分片和记录都是 ...
- 【jsoi】第一季 [略]精简题解
UPD:好像有两道题的代码逃跑了?= =就先不找了,反正都是水题. 精简题解系列第四弹.(其实也不是那么精简啊= =) [JSOI2008]最大数maxnumber 单点修改,区间最大值查询,裸线段树 ...
- json01-json简介和语法
JSON: JavaScript Object Notation(JavaScript 对象表示法) JSON 是存储和交换文本信息的语法.类似 XML,但比 XML 更小.更快,更易解析,是轻量级的 ...
- hdoj1002--A + B Problem II
Problem Description I have a very simple problem for you. Given two integers A and B, your job is to ...
- iso不支持document事件
ios safari游览器除了a.input.button等不支持document事件委托?<body>加上这个样式即可 <style> .clickable-div { cu ...