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 ...
随机推荐
- RK3288 HDMI配置和调试
RK3288 最大输出分辨率为 3840x2160 HDMI 驱动代码位于 kernel/drivers/video/rockchip/hdmi/rockchip-hdmiv2 目录 1.设置默认输出 ...
- 启动Oracle数据库时报错ORA-00119 & ORA-00132
今天启动Oracle数据库时报错ORA-00119 & ORA-00132,找到解决方法做个记录,方便日后查看. 若是ORACLE不提示错误的话,可以自己查看ORACLE的日志文件. Orac ...
- C#如何:启用和禁用自动绑定重定向 (微软)
https://msdn.microsoft.com/zh-cn/library/2fc472t2.aspx 如何:启用和禁用自动绑定重定向 .NET Framework (current versi ...
- DP 过河卒
棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. 棋盘用坐标 ...
- coding style 的两点
通俗介绍coding style两点建议: 模块划分 这个如果做得不好,简直不能忍.有的代码非常莫名其妙,有些东西本身不复杂,非要将其拆成好几个部分,然后做成一个一个碎散的模块,这样并不好.举个例子, ...
- 同步机制之--java之CountDownLatch闭锁
CountDownLatch闭锁 1.类介绍 一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待.用给定的计数初始化 CountDownLatch.CountDown ...
- python算两个时间之间的天数,将天数转成int型
import time import datetime #计算两个日期相差天数,自定义函数名,和两个日期的变量名. def Caltime(date1,date2): #%Y-%m-%d为日期格式,其 ...
- Java-Runoob-高级课程:Java 8 新特性
ylbtech-Java-Runoob-高级课程:Java 8 新特性 1.返回顶部 1. Java 8 新特性 Java 8 (又称为 jdk 1.8) 是 Java 语言开发的一个主要版本. Or ...
- 第十二章 Ganglia监控Hadoop及Hbase集群性能(安装配置)
1 Ganglia简介 Ganglia 是 UC Berkeley 发起的一个开源监视项目,设计用于测量数以千计的节点.每台计算机都运行一个收集和发送度量数据(如处理器速度.内存使用量等)的名为 gm ...
- python学习(二十六) 字典
# 访问字典car = {'make':'bmw', 'model':'550i', 'year':2016}print(car)print(car['make']) # 添加元素d = {}d['o ...