题目描述:

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

思路:

给定一个字符串代表机器人移动的方向,U表示上,D表示下,L表示左,R表示右,判断最后是否回到原点
*设置原点为(0,0),x表示横坐标,y表示纵坐标

 1 public class Solution657 {
 2      public boolean judgeCircle(String moves) {
 3             int x = 0,y = 0;
 4             for(int i = 0; i < moves.length();i++){
 5                 if(moves.charAt(i)=='U') y++;
 6                 if(moves.charAt(i)=='D') y--;
 7                 if(moves.charAt(i)=='L') x--;
 8                 if(moves.charAt(i)=='R') x++;
 9             }
10             if(x==0&&y==0) return true;
11             else return false;
12         }
13     public static void main(String[] args) {
14         // TODO Auto-generated method stub
15         Solution657 solution657 = new Solution657();
16         String moves = "UDUDLRL";
17         if(solution657.judgeCircle(moves)==true){
18             System.out.println(1);
19         }else {
20             System.out.println(0);
21         }
22     }
23
24 }

转载于:https://www.cnblogs.com/zlz099/p/8507625.html

[LeetCode]657.Judge Route Circle相关推荐

  1. LeetCode 657. Judge Route Circle

    题目: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this rob ...

  2. 【Lintcode】1104. Judge Route Circle

    题目地址: https://www.lintcode.com/problem/judge-route-circle/description 在平面直角坐标系里,以方向给一个出发点在(0,0)(0,0) ...

  3. Judge Route Circle

    这道题为简单题,网上通过率接近70%,我就不怎么仔细讲了 题目: 思路: 题目太简单,我也没怎么多想,直接暴力解决,然后就导致时间运行太慢 = = 代码: 1.这是我的写的,很菜 1 class So ...

  4. [leetcode] online judge题目汇总

    最近在leetcode online judge刷题,132道题也做了一半多,略有心得.另外这个online judge对题目没有分类,也没有说明难度,新人上手无法由浅入深,也不能集中练习某些类型的题 ...

  5. LeetCode 657 : Robot Return to Origin

    题目地址:Robot Return to Origin - LeetCode Acceptance:70.8% Difficulty:Easy There is a robot starting at ...

  6. [LeetCode Online Judge]系列-求二维平面内在一条直线上的最大点数

    2019独角兽企业重金招聘Python工程师标准>>> Max Points on a Line Given n points on a 2D plane, find the max ...

  7. LeetCode 657. 机器人能否返回原点

    文章目录 1. 题目 2. 解题 1. 题目 在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字 ...

  8. LeetCode Online Judge 题目C# 练习 - Minimum Path Sum

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  9. LeetCode Online Judge 题目C# 练习 - Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

最新文章

  1. 【sas proc sql】coalesce
  2. Python3.4连接Mysql
  3. 被问到一个简单的脚本后
  4. 前用户sudo免密码
  5. 进程以及状态 进程的创建
  6. linux环境编程apue和unp,UNIX高级环境编程:源码(apue)下载 编译和运行
  7. 计算机p除c,计算机【电脑】中,Ctrl S/C/P/PrtSc/break/numberlock.分别表明什么?
  8. 接口快速开发框架 magic-api 2.x初体验
  9. 初中计算机课师徒结对活动记录,2013师徒结对活动记录
  10. 债券数据集:绿色债券数据集、历时新发、发行债券、DCM定价估值四大指标数据
  11. java 全角_java 全角半角字符转换的方法实例
  12. 深入理解JVM - ZGC垃圾收集器
  13. C#.NET程序设计教程实验指导(清华大学 江红,余青松)实验源码第六章
  14. 慕课网_《Redis入门》学习总结
  15. DLL开发之得到注册码及其使用
  16. 地图经纬度纠偏-所有高德系地图纠偏
  17. 重装系统后如何恢复php环境
  18. 2021年电工(初级)模拟考试题及电工(初级)模拟考试题库
  19. SVG动画编程及其应用
  20. 麒麟V10无法安装PC版微信的解决办法及系统更新方法

热门文章

  1. 什么是公有云、私有云、混合云?
  2. STM32L0 读取芯片温度与当前供电电压 STM32L051C8T6
  3. Jessica Kerr:高绩效团队简史
  4. XML学习笔记(三)
  5. node process
  6. 【Bootstrap】 bootstrap-table表格组件
  7. sshclientCRT连接linux使用技巧
  8. 哲学上的终极问题:你在追求什么?
  9. 修改打开方式的程序列表中列出程序的名称
  10. 软件测试 学习之路 Linux基础命令 (一)