Love丶FFC's Blog

LintCode_easy:机器人能否返回原点

2019-08-31 12:10:14
阅读:563   •   评论:6
标签:,

描述

最初,机器人位于(0, 0)处。 给定一系列动作,判断该机器人的移动轨迹是否是一个环,这意味着它最终会回到原来的位置。

移动的顺序由字符串表示。 每个动作都由一个字符表示。 有效的机器人移动是R(右),L(左),U(上)和D(下)。 输出应该为true或false,表示机器人是否回到原点。

样例

样例1:

输入: "UD"
输出: true

样例2:

输入: "LL"
输出: false

编程语言:Java

解题思想:

1.以坐标的形式来移动位置和判断位置。

2.因为只有四种走法,可以使用switch进行判断。

时间复杂度:O(n)

IDE代码如下:

  1. package LintCode_Easy;
  2.  
  3. public class _1104{
  4.  
  5. public static void main(String[] args) {
  6. String moves="LL";
  7. int xplace = 0,yplace=0;
  8. for(int i=0;i<moves.length();i++)
  9. {
  10. switch (moves.charAt(i)) {
  11. case 'U':yplace++;break;
  12. case 'D':yplace--;break;
  13. case 'L':xplace--;break;
  14. case 'R':xplace++;break;
  15. default:
  16. break;
  17. }
  18. }
  19. boolean placejudge=(xplace==0 && yplace==0)?true:false;
  20. System.out.println(placejudge);
  21. }
  22. }

LintCode编辑器代码如下:

  1. public class Solution {
  2. public boolean judgeCircle(String moves) {
  3. int xplace = 0,yplace=0;
  4. for(int i=0;i<moves.length();i++)
  5. {
  6. switch (moves.charAt(i)) {
  7. case 'U':yplace++;break;
  8. case 'D':yplace--;break;
  9. case 'L':xplace--;break;
  10. case 'R':xplace++;break;
  11. default:
  12. break;
  13. }
  14. }
  15. boolean placejudge=(xplace==0 && yplace==0)?true:false;
  16. return placejudge;
  17. }
  18. }

评论板

共有 6 条评论

  1. Abupguele

    LASIX and potassium help buy zithromax online canada Bella, USA 2022 06 28 08 05 03

  2. Outlils

    How Veterinarians Diagnose Heartworm Disease in Cats cialis order online 040, respectively Fig

  3. invoimi

    Adverse events were adjudicated by an independent, blinded clinical events committee safe cialis online

  4. invoimi

    We were warned that it could take several cycles of treatment to be successful buy finasteride 1mg

  5. NBfcKdi

    what would happen if a girl took a viagra pill Patients with intraductal noninvasive carcinomas were not eligible for our trial

  6. hsvfTBms

    Jamar VbmLDayJtDufVmUx 6 17 2022 levitra 20 levitra 20 mg

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:508365