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 ...
随机推荐
- Oracle分组函数实例
分组函数也叫聚合函数.如果在查询只想要查分组函数,那么跟平时的查询语句并无不同: SQL ,,,,) ; SUM(T.PRIZENUM) AVG(T.PRIZENUM) --------------- ...
- SecureCRT导入已有会话
如果别人有完整的环境信息,我们想拿过来,怎么导入?或者别人想要我的会话配置信息,怎么导出?对SecureCRT这个工具来说很easy,根本不需要去找什么导入.导出按钮,直接文件操作. 假如我的Secu ...
- NumPy-快速处理数据--ufunc运算--广播--ufunc方法
本文摘自<用Python做科学计算>,版权归原作者所有. 1. NumPy-快速处理数据--ndarray对象--数组的创建和存取 2. NumPy-快速处理数据--ndarray对象-- ...
- vs2015安装ORACLE的DbFirst
不说DbFirst好在哪里,它和ModelFirst,CodeFirst都各有各的好,由于对于已经存在的一个大型的业务库,使用EntityFramework的更倾向于DbFirst,因为好多同事已经习 ...
- js中去掉字符串中的某个指定字符
假设一个data里面的数据是[tian,12],现在去掉[],代码如下 data=data.replace("[",""); data=data.replace ...
- Exce信息提取
Exce信息提取 Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub 信息汇总( ...
- "==" 与 “equals”
“==”: “==”或等号操作在Java编程语言中是一个二元操作符,用于比较原生类型和对象.(操作符不支持重载overloading) “==”对比两个对象基于内存引用,如果两个对象的引用完全相同(指 ...
- java代码----------实现创建DataInputStream和DataOutputStream进行读写
总结: 主要是 捕获异常 package com.a.b; import java.io.*; public class testData { public static void main(Stri ...
- ping正常但是ssh到linux服务器很卡的解决方法
ssh登录很慢的问题 1.关闭ssh DNS反向解析 vi /etc/ssh/sshd_config 修改UseDNS no 2.关闭 GSSAPI 的用户认证 vi /etc/ssh/sshd ...
- PHP中使用mkdir创建多级目录的方法
function mkdirs($dir) { if(!is_dir($dir)) { if(!mkdirs(dirname($dir))){ return false; ...