资讯专栏INFORMATION COLUMN

【LeetCode】657. Judge Route Circle

Shihira / 2291人阅读

摘要:题目描述解决方案解题思路设置初始坐标为根据上下左右指示调整坐标判断最后坐标的位置是否为起始位置。加强版循环使用比判断快方法计算向左和向右的次数是否相同,计算向上和向下的次数相同。若都相同,则回到原地。

题目描述

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
解决方案 - C++
class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0, y = 0;
        for(int i = 0; i < moves.length(); i++){
            if(moves.substr(i, 1) == "R"){
                x--;
            }
            if(moves.substr(i, 1) == "L"){
                x++;
            }
            if(moves.substr(i, 1) == "U"){
                y++;
            }
            if(moves.substr(i, 1) == "D"){
                y--;
            }
        }
        if(x == 0 && y == 0){
            return true;
        }else{
            return false;
        }
    } 
};

解题思路
设置初始坐标为(0,0),根据上UDLR指示调整坐标,判断最后坐标的位置是否为起始位置。

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 39 ms
[C++] [Java] Clean Code

Tips:

1.加强版for循环
2.使用switchif判断快
3.Java toCharArray()方法

C++

class Solution {
public:
    bool judgeCircle(string moves) {
        int v = 0;
        int h = 0;
        for (char ch : moves) {
            switch (ch) {
                case "U" : v++; break;
                case "D" : v--; break;
                case "R" : h++; break;
                case "L" : h--; break;
            }
        }
        return v == 0 && h == 0;
    }
};

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 19 ms

Java

class Solution{
    public boolean judgeCircle(String moves) {
        int v = 0, h = 0;
        for (char move : moves.toCharArray()) {
            switch (move) {
                case "U": v++; break;
                case "D": v--; break;
                case "R": h++; break;
                case "L": h--; break;
            }
        }
        return v == 0 && h == 0;
    } 
}

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 11 ms 
Python one liner
class Solution(object):
    def judgeCircle(self, moves):
        """
        :type moves: str
        :rtype: bool
        """ 
        return moves.count("L") == moves.count("R") and moves.count("U") == moves.count("D")

Submission Details

62 / 62 test cases passed.
Status: Accepted
Runtime: 42 ms 

计算向左和向右的次数是否相同,计算向上和向下的次数相同。若都相同,则回到原地。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67723.html

相关文章

  • Leetcode PHP题解--D9 657. Robot Return to Origin

    摘要:题目链接题目分析输入一串指令操作机器人,判断执行完指令后,能否回到原点。思路判断向上移动的次数是否等于向下移动的次数,且向左次数是否等于向右次数。但是,如果在指令中没有出现所有种方向的话,在判断时会获取不到数值。 657. Robot Return to Origin 题目链接 657. Robot Return to Origin 题目分析 输入一串指令操作机器人,判断执行完指令后,...

    Paul_King 评论0 收藏0
  • #yyds干货盘点#“愚公移山”的方法解atoi,自以为巧妙!

    摘要:若函数不能执行有效的转换,返回。如果数值超过可表示的范围,则返回或。示例输入输出解释转换截止于数字,因为它的下一个字符不为数字。 这是我参与11月更文挑战的第12天。一、写在前面LeetCode 第一题两数之和传输门:听说你还在写双层for循环解两数之和?LeetCode 第二题两数之和传输门:两个排序数组的中...

    番茄西红柿 评论0 收藏2637
  • LeetCode 520 检测大写字母[模拟] HERODING的LeetCode之路

    摘要:解题思路模拟题就按照题目意思来做题目分三种情况那我们也分三种情况通过设置用来标志第一位是大写还是小写小写的话直接通过后面的都不能大写来判断大写的话用记录后面的大写的个数为或者为字符串长度才符合要求代码如下代码 ...

    番茄西红柿 评论0 收藏2637
  • [Leetcode] Gray Code 格雷码

    摘要:找规律复杂度时间空间思路仔细观察格雷码当时当时当时可以发现,的格雷码,就是的格雷码,再加上它们的逆序前面多一个。 Grey Code The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n re...

    Code4App 评论0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 评论0 收藏0

发表评论

0条评论

Shihira

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<