leetcode-罗马数字转整数
  MJlmRDrYd0Ow 2023年11月02日 39 0


typedef struct special_roman {
const char *roman;
int value;
} special_roman;
const int SPECIAL_ROMAN_LEN = 6;
special_roman romans[SPECIAL_ROMAN_LEN] = { {"IV", 4}, {"IX", 9}, {"XL", 40}, {"XC", 90}, {"CD", 400}, {"CM", 900} };
std::map<char, int>common_roman = { {'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000} };
class Solution {
public:
int romanToInt(string s) {
int sum = 0;
for (auto &r : romans) {
auto pos = s.find(r.roman);
if (pos != std::string::npos) {
sum += r.value;
s.erase(pos, 2);
}
}
for (auto &ch : s) {
sum += common_roman[ch];
}
return sum;
}
};

​https://github.com/wangzhicheng2013/leetcode/tree/main/leetcode​

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  cOi2LVubBNG7   2023年11月02日   82   0   0 github服务器git
  nwrHrkoQE0C4   2023年11月02日   55   0   0 github负载均衡nginx
  8KhYbgszLLmZ   2023年11月02日   95   0   0 htmlgithubnginx
  5b99XfAwWKiH   2023年11月12日   35   0   0 githubC++openrmcfish
MJlmRDrYd0Ow