657. Robot Return to Origin
Description
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
Example:
Input: "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
分析:
- 设置x = 0, y = 0;
- 一次检查每一个字符,分别改变x,y的值
- 最后查看x、y是否都是0
class Solution {
public boolean judgeCircle(String moves) {
Robot r = new Robot();
char[] chars = moves.toCharArray();
for(char c: chars){
if(c == 'R') r.x++;
else if(c == 'L') r.x--;
else if(c == 'U') r.y++;
else r.y--;
}
if(r.x == 0 && r.y == 0) return true;
else return false;
}
class Robot{
int x;
int y;
public Robot(){
this.x = 0;
this.y = 0;
}
}
}
657. Robot Return to Origin的更多相关文章
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...
- LeetCode 657. Robot Return to Origin
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
- LeetCode 657 Robot Return to Origin 解题报告
题目要求 There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of it ...
- LeetCode 657. Robot Return to Origin (C++)
题目: There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its ...
- 【LeetCode】657. Robot Return to Origin 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...
- LeetCode #657. Robot Return to Origin 机器人能否返回原点
https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...
- LeetCode--Squares of a Sorted Array && Robot Return to Origin (Easy)
977. Squares of a Sorted Array (Easy)# Given an array of integers A sorted in non-decreasing order, ...
- [Swift]LeetCode657. 机器人能否返回原点 | Robot Return to Origin
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
随机推荐
- Hyper-V 配置虚拟网络
Hyper-V功能异常强大,不了解的自行GG 本文介绍如何使用Hyper-V在本机建立虚拟网络,创建虚拟交换机 打开Hyper-V Manager,选择右边侧边栏的Virtual Switch Man ...
- 【比赛】NOIP2018 填数游戏
打表找规律.... #include<bits/stdc++.h> #define ui unsigned int #define ll long long #define db doub ...
- 利用ansible批量部署zabbix-agent
应用环境:Linux运维工作少不了一个好的监控,zabbix就是目前比较好的一款开源监控软件. 监控类型多种多样,如果不介意或者系统支持安装,那么agent方式是首选. 当主机数量较多时,可以利用相关 ...
- centos7系统排错
系统排错 troubleshooting winPE --光盘或u盘启动盘 产生一个PE系统(类似内存上跑的临时系统) 系统排错 rescue 模式 (挽救模式) 类似windows winPE模式 ...
- margin纵向重叠
速记: 如p的纵向 margin 是 16px,那么两个之间纵向的距离是多少?-- 按常理来说应该是 16 + 16 = 32px,但是答案仍然是 16px. 因为纵向的 margin 是会重叠的,如 ...
- bat 复制文件夹,文件名递增 等操作
句尾无';' @echo off : 回显,使命令不在dos中一行一行输出 pause : 暂停,以便看到输出结果 变量 %% 与 % % : https://zhidao.baidu.com/que ...
- http 请求头和响应头
客户端发送请求过程带着的数据: 1.请求地址 2.请求方式 3.请求头 request headers 4.请求参数 https://www.juhe.cn/ 130.... 1a2b....pei ...
- crond守护进程实现定时监控某进程占有内存的大小
1)添加计划任务 crontab -e会使用某个编辑器打开某个文件,然后在内输入需要执行的计划任务,保存后在/var/spool/cron/crontabs/下会出现以用户名命名的文件 2)计划任务如 ...
- RabbitMQ入门-竞争消费者模式
上一篇讲了个 哈喽World,现在来看看如果存在多个消费者的情况. 生产者: package com.example.demo; import com.rabbitmq.client.Channel; ...
- JAVA设计模式初探之适配器模式
http://blog.csdn.net/jason0539/article/details/22468457 1. 概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接 ...