zoukankan      html  css  js  c++  java
  • LeetCode 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
    

    题目标签:String

      

      距离上一次刷题已经是一个月之前了- -,因为终于找到了工作。上班一个月的感受是,练好英语很重要,比起写代码水平,首先你得把开会说的那些都听懂是吧。所以大家要把对英语的重视程度提高到和刷题一样!!!

      
     
      题目让我们来判定,一个机器人从出发点开始以 “上”, “下”, “左”, “右” 的方式来移动,最后是否回到了原点。
      这一题还是挺容易的,只要把出发点设为x = 0, y = 0 ,然后把每一次的移动 加上1 或者减去1 就可以了。
      具体请看Code。
     
     
     

    Java Solution:

    Runtime beats 72.84% 

    完成日期:03/24/2018

    关键词:坐标

    关键点:出发点为 x = 0, y = 0

     1 class Solution 
     2 {
     3     public boolean judgeCircle(String moves) 
     4     {
     5         int x = 0;
     6         int y = 0;
     7         
     8         for(char c: moves.toCharArray())
     9         {
    10             switch(c)
    11             {
    12                 case 'R':
    13                     x++;
    14                     break;
    15                 case 'L':
    16                     x--;
    17                     break;
    18                 case 'U':
    19                     y++;
    20                     break;
    21                 case 'D':
    22                     y--;
    23                     break;
    24                 default:
    25                     System.out.println("Invalid move");
    26             }
    27         }
    28         
    29         
    30         return (x == 0 && y == 0);
    31     }
    32 }

    参考资料:n/a

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    加入创业公司有什么利弊
    Find Minimum in Rotated Sorted Array II
    Search in Rotated Sorted Array II
    Search in Rotated Sorted Array
    Find Minimum in Rotated Sorted Array
    Remove Duplicates from Sorted Array
    Spiral Matrix
    Spiral Matrix II
    Symmetric Tree
    Rotate Image
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/8642859.html
Copyright © 2011-2022 走看看