【Leetcode_easy】657. Robot Return to Origin

problem

657. Robot Return to Origin

题意:

solution1:

class Solution {
public:
    bool judgeCircle(string moves) {
        int cnt1 = 0, cnt2 = 0;
        for(auto move:moves)
        {
            if(move == 'L') cnt1++;
            else if(move=='R') cnt1--;
            else if(move=='U') cnt2++;
            else if(move=='D') cnt2--;
        }
        if(cnt1==0 && cnt2==0) return true;
        else return false;
    }
};

solution2:

class Solution {
public:
    bool judgeCircle(string moves) {
        unordered_map<char, int> mymap;
        for(auto move:moves) mymap[move]++;
        //return (mymap.count('L')==mymap.count('R') && mymap.count('U')==mymap.count('D'));
        return mymap['L']==mymap['R'] && mymap['U']==mymap['D'];
    }
};

 

参考

1. Leetcode_easy_657. Robot Return to Origin;

 

posted on 2019-06-26 17:12  鹅要长大  阅读(143)  评论(0编辑  收藏  举报

导航