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.

分析:

  1. 设置x = 0, y = 0;
  2. 一次检查每一个字符,分别改变x,y的值
  3. 最后查看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的更多相关文章

  1. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  2. 【Leetcode_easy】657. Robot Return to Origin

    problem 657. Robot Return to Origin 题意: solution1: class Solution { public: bool judgeCircle(string ...

  3. 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 ...

  4. 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 ...

  5. 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 ...

  6. 【LeetCode】657. Robot Return to Origin 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 复数求和 counter统计次数 相似题目 参考资料 ...

  7. LeetCode #657. Robot Return to Origin 机器人能否返回原点

    https://leetcode-cn.com/problems/robot-return-to-origin/ 设置 flagUD 记录机器人相对于原点在纵向上的最终位置 flagRL 记录机器人相 ...

  8. 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, ...

  9. [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 ...

随机推荐

  1. Hdoj 1253.胜利大逃亡 题解

    Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个ABC的立方体,可以被表示成A个B*C的矩 ...

  2. [SHOI2014]概率充电器(概率+换根dp)

    著名的电子产品品牌SHOI 刚刚发布了引领世界潮流的下一代电子产品—— 概率充电器: “采用全新纳米级加工技术,实现元件与导线能否通电完全由真随机数决 定!SHOI 概率充电器,您生活不可或缺的必需品 ...

  3. Xposed+JustTrustMe+Android

    场景介绍:APP抓包 引出的知识点:ssl-pinning. ssl-pinning: apk在开发时就将服务端证书一块打包到客户端里.这样在HTTPS建立时与服务端返回的证书比对一致性,进而识别出中 ...

  4. 关于Autosar中DCM(14229UDS)模块的理解

    阅读本篇文章希望达到的目的是: UDS是干什么的, ISO14229是如何定义规则的, 希望接下来的阅读让你不虚此行. 1. UDS是干什么的?UDS全称是Unified Diagnostic Ser ...

  5. 2018 ICPC 焦作网络赛 E.Jiu Yuan Wants to Eat

    题意:四个操作,区间加,区间每个数乘,区间的数变成 2^64-1-x,求区间和. 题解:2^64-1-x=(2^64-1)-x 因为模数为2^64,-x%2^64=-1*x%2^64 由负数取模的性质 ...

  6. squid详解(正向代理、透明代理、反向代理)

    squid http://www.squid-cache.org/ --官方网址 squid软件主要有两大应用:1,代理上网(正向代理,透明代理) 2,网站静态页面缓存加速(反向代理) 三种代理类型: ...

  7. Linux脚本点滴

    1.当硬盘空间不足15%时,循环删除最早的日志文件(ELK): #!/bin/bash for((i=20;i>=5;i-=1)) do code=`df /dev/mapper/ElkGrou ...

  8. axios请求、返回拦截器

    1.http 请求拦截器 axios.interceptors.request.use(function(config){ //在发送请求之前做些什么 return config }), functi ...

  9. ImageMagick 笔记: 索引颜色(index color)、锁定图层,透明 png 转 gif (保持清晰度)

    今天在处理一张 png 透明背景的图片,大小: 16KB, 尺寸: 400 x 300,用到一段代码,也许对以后有用. /** 带透明背景和阴影的png图片, 转换成 gif, [索引色] + [锁住 ...

  10. python(一)——初识与变量

    ---恢复内容开始--- #!/usr/bin/env python #-*- coding:utf8 -*- print('你好hello world') 第一行,主要用于Linux中环境变量,wi ...