当前位置:网站首页>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;
}
};
边栏推荐
- 【补题】2021牛客暑期多校训练营6-n
- 电子学:第010课——实验 9:时间与电容器
- 时钟刻度盘的绘制
- FFT【模板】
- Luogu p3313 [sdoi2014] travel (tree chain + edge weight transfer point weight)
- What problems do you worry about when you want to switch to software testing?
- Deep learning series 48:deepfaker
- Self made ramp, but it really smells good
- TS environment setup
- Remove headers from some pages in a word document
猜你喜欢
Use Adobe Acrobat pro to resize PDF pages
Dietary intervention reduces cancer treatment-related symptoms and toxicity
测一测现在的温度
飞机引气系统的建模与故障仿真
Luogu p1073 [noip2009 improvement group] optimal trade (layered diagram + shortest path)
A solution to slow startup of Anaconda navigator
c#磁盘驱动器及文件夹还有文件类的操作
共话云原生数据库的未来
CVPR 2022 oral 2D images become realistic 3D objects in seconds
Biweekly investment and financial report: capital ambush Web3 infrastructure
随机推荐
DNS protocol and its complete DNS query process
家庭服务器门户Easy-Gate
每日刷题记录 (三)
Not afraid of losing a hundred battles, but afraid of losing heart
Opencv minimum filtering (not limited to images)
Free SSL certificate acquisition tutorial
現在通過開戶經理發的開戶鏈接股票開戶安全嗎?
使用pytorch搭建MobileNetV2并基于迁移学习训练
Luogu p6822 [pa2012]tax (shortest circuit + edge change point)
黑点==白点(MST)
飞机引气系统的建模与故障仿真
FFT【模板】
现在通过开户经理发的开户链接股票开户安全吗?
[supplementary question] 2021 Niuke summer multi school training camp 4-N
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
牛客:飞行路线(分层图+最短路)
数论模板啊
A solution to slow startup of Anaconda navigator
Socket problem record