problem
题意:
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;
完