leetcode657. 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

思路:比较简单,主要是一次遍历字符串,遇到RLUD就记录下来,因为最后要返回原点,所以上下之和或左右之和都必须为0,累加到最后判断即可.

class Solution {
public:
    bool judgeCircle(string moves) {
        int count = 0;
        for(int i = 0; i < moves.size(); i++) {
            switch(moves[i]){
                case 'R' : count += 1; break;
                case 'L' : count += -1; break;
                case 'U' : count += 2; break;
                case 'D' : count += -2; break;
            }
        }
        if(count == 0) return true;
        else return false;
    }
};
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 星耀小学六年一班刘美彤读书时长四十分钟,第一天亲子共同成长 星耀小学六年一班刘美彤读书时长四十分钟,第二天亲子共同...
    艳的简单阅读 2,953评论 0 2
  • 无论发生什么样的事情,你不说是不会有人知道的。 今天只有我和a上早班,开会分配任务。 a:“你今天打扫全卖场的卫生...
    邓锦双阅读 1,898评论 4 0
  • 当与一个人相处久了,在两人的情感世界里,缺点也会变成有趣的东西。
    keymale阅读 1,111评论 0 1
  • 龚小菲打开了房门,人显得萎靡。几十天的感情折磨和工作的压力摧残了她的身心!犹如一颗果树上新鲜的桃子,落到了地上,无...
    雨射零丁阅读 2,895评论 0 1