657. Judge Route Circle机器人能否返回
[抄题]:
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
时间分析:
空间分析:n
[优化后]:因为判断抵消效应,只用一个变量++--足矣
时间分析:
空间分析:1
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
转化成字符串数组后再操作
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
判断抵消效应只用一个变量就行了
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public boolean judgeCircle(String moves) {
//cc
if (moves == null) {
return false;
}
//array
int x = 0, y = 0;
for (char c : moves.toCharArray()) {
if (c == 'R') {
x++;
}
if (c == 'L') {
x--;
}
if (c == 'U') {
y++;
}
if (c == 'D') {
y--;
}
}
//return x && y
return (x == 0 && y == 0);
}
}
657. Judge Route Circle机器人能否返回的更多相关文章
- 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 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 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 ...
- 657. Judge Route Circle
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 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 ...
- [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 Judge Route Circle
原题链接在这里:https://leetcode.com/problems/judge-route-circle/description/ 题目: Initially, there is a Robo ...
随机推荐
- python time模块 sys模块 random模块
1,time模块 python中的内置模块 #1,显示当前时间戳 print(time.time()) #2,字符串格式化 print(time.strftime('%Y-%m-%d-%H-%M-%S ...
- oracle之 调整 I/O 相关的等待
I/O相关竞争等待简介 当Oracle数据库出现I/O相关的竞争等待的时候,一般来说都会引起Oracle数据库的性能低下,发现数据库存在I/O相关的竞争等待一般可以通过以下的三种方法来查看Oracle ...
- c++ 异常处理(3)
<C++编码规范与指导>一文,就已经规划着要加入这样一篇讨论 C++ 异常机制的文章了.没想到时隔几年以后才有机会把这个尾巴补完 :-). 还是那句开场白:“在恰当的场合使用恰当的特性” ...
- Android多线程断点下载的代码流程解析
Step 1:创建一个用来记录线程下载信息的表 创建数据库表,于是乎我们创建一个数据库的管理器类,继承SQLiteOpenHelper类 重写onCreate()与onUpgrade()方法 DBOp ...
- STL算法与树结构模板
STL算法 STL 算法是一些模板函数,提供了相当多的有用算法和操作,从简单如for_each(遍历)到复杂如stable_sort(稳定排序),头文件是:#include <algorithm ...
- 第五章 MyEclipse配置hadoop开发环境
1.首先要下载相应的hadoop版本的插件,我这里就给2个例子: hadoop-1.2.1插件:http://download.csdn.net/download/hanyongan300/62381 ...
- 一些Java相关的
都是从<Thinking in Java>英文第四版中摘抄的 _______________________________________________________________ ...
- 硬盘IOPS与读写速度
IOPS (Input/Output Per Second)即每秒的输入输出量(或读写次数),是衡量磁盘性能的主要指标之一.IOPS是指单位时间内系统能处理的I/O请求数量,一般以每秒处理的I/O请求 ...
- 初识python(python的安装与运行)
python--“优雅”.“明确”.“简单”的哲学定位 一.python的安装(Windows环境下) 1.在python官网下载安装文件 python的官方网址:https://www.python ...
- Cesium有价值网址
//比较粗 https://www.cnblogs.com/mazhenyu/p/6494748.html //很详细 2019.4.19 https://www.cnblogs.com/fuckgi ...