当前位置:网站首页>leetcode.13 --- 罗马数字转整数
leetcode.13 --- 罗马数字转整数
2022-06-25 06:51:00 【_End丶断弦】
罗马数字转整数

12题颠倒一下,利用哈希表解决。将罗马数字和它对应的数值存进哈希表,遍历罗马数字,若当前位置的罗马数字比它后面的小则需要减去当前的罗马数字,否则加上当前的罗马数字。
代码如下:
class Solution {
public:
int romanToInt(string s) {
unordered_map<char,int> hash;
hash['I'] = 1;hash['V'] = 5;hash['X'] = 10;hash['L'] = 50;hash['C'] = 100;
hash['D'] = 500;hash['M'] = 1000;
int res = 0;
for(int i = 0;i < s.size();i++)
{
if(i+1 < s.size() && hash[s[i]] < hash[s[i+1]])
res -= hash[s[i]];
else
res += hash[s[i]];
}
return res;
}
};
边栏推荐
- c#磁盘驱动器及文件夹还有文件类的操作
- 420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth
- Niuke: flight route (layered map + shortest path)
- STM32CubeMX 学习(5)输入捕获实验
- The era of enterprise full cloud -- the future of cloud database
- Ffmpeg+sdl2 for audio playback
- How to create a new branch with SVN
- How do I install the software using the apt get command?
- 电子学:第011课——实验 10:晶体管开关
- Est - il sûr d'ouvrir un compte d'actions maintenant via le lien d'ouverture de compte coiffé?
猜你喜欢

420 sequence traversal of binary tree 2 (429. sequence traversal of n-ary tree, 515. find the maximum value in each tree row, 116. fill in the next right node pointer of each node, 104. maximum depth

使用apt-get命令如何安装软件?

STM32CubeMX 學習(5)輸入捕獲實驗

面试前准备好这些,Offer拿到手软,将军不打无准备的仗

电子学:第010课——实验 8:继电振荡器

Deep learning series 45: overview of image restoration

使用pytorch搭建MobileNetV2并基于迁移学习训练

2022年毕业生求职找工作青睐哪个行业?

静态网页服务器

电子学:第012课——实验 11:光和声
随机推荐
Electronics: Lesson 009 - Experiment 7: study relays
Debugging mipi-dsi screen based on stm32mp157
图像超分综述:超长文一网打尽图像超分的前世今生 (附核心代码)
飞机引气系统的建模与故障仿真
Self made ramp, but it really smells good
牛客:飞行路线(分层图+最短路)
洛谷P2486 [SDOI2011]染色(树链+线段树 + 树上区间合并 )
Is it safe to open an account through the haircut account opening link now?
自制坡道,可是真的很香
STM32CubeMX 學習(5)輸入捕獲實驗
家庭服务器门户Easy-Gate
TCP 加速小记
电子学:第008课——实验 6:非常简单的开关
数论模板啊
【红旗杯?】补题
Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
Apache CouchDB 代码执行漏洞(CVE-2022-24706 )批量POC
DNS protocol and its complete DNS query process
PH neutralization process modeling
剑指offer刷题(中等等级)
