TreeviewCopyright © aleen42 all right reserved, powered by aleen42

657. 机器人能否返回原点

https://leetcode-cn.com/problems/robot-return-to-origin/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-29 13:57:43
 * @LastEditTime: 2020-08-29 13:58:01
 * @Description: https://leetcode-cn.com/problems/robot-return-to-origin/
 * @FilePath: \leetcode-googtech\#657. Robot Return to Origin\Solution.java
 * @WebSite: https://algorithm.show/
 */

class Solution {
    public boolean judgeCircle(String moves) {
        int col = 0, row = 0;
        for(char ch : moves.toCharArray()) {
            if(ch == 'U') row++;
            else if(ch == 'D') row--;
            else if(ch == 'L') col++;
            else col--;
        }
        return row == 0 && col == 0;
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-29 13:57:51
LastEditTime: 2020-08-29 13:58:16
Description: https://leetcode-cn.com/problems/robot-return-to-origin/
FilePath: \leetcode-googtech\#657. Robot Return to Origin\Solution.py
WebSite: https://algorithm.show/
'''

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')
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""