力扣題-12.4
[力扣刷題攻略] Re:從零開始的力扣刷題生活
力扣題1:657. 機器人能否返回原點
解題思想:進行統計即可
class Solution(object):def judgeCircle(self, moves):""":type moves: str:rtype: bool"""dic = {'U': 0, 'D': 0, 'R': 0, 'L': 0}for move in moves:if move == 'U':dic['U'] += 1elif move == 'D':dic['D'] += 1elif move == 'R':dic['R'] += 1elif move == 'L':dic['L'] += 1return dic['U'] == dic['D'] and dic['R'] == dic['L']
class Solution {
public:bool judgeCircle(string moves) {std::unordered_map<char, int> counts{{'U', 0}, {'D', 0}, {'R', 0}, {'L', 0}};for (char move : moves) {if (move == 'U') {counts['U']++;} else if (move == 'D') {counts['D']++;} else if (move == 'R') {counts['R']++;} else if (move == 'L') {counts['L']++;}}return counts['U'] == counts['D'] && counts['R'] == counts['L'];}
};