力扣题-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'] += 1
elif move == 'D':
dic['D'] += 1
elif move == 'R':
dic['R'] += 1
elif move == 'L':
dic['L'] += 1
return 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'];
}
};