当前位置:网站首页>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;
}
};
边栏推荐
- 洛谷P2839 [国家集训队]middle(二分 + 主席树 + 区间合并)
- Determine whether the user is entering a page for the first time
- 五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)
- 黑点==白点(MST)
- 飞机引气系统的建模与故障仿真
- Thread + thread problem record
- Static web server
- RMQ interval maximum subscript query, interval maximum
- Network model -- OSI model and tcp/ip model
- 【补题】2021牛客暑期多校训练营9-n
猜你喜欢

Matlab code format one click beautification artifact

Stm32cubemx Learning (5) Input capture Experiment

Electronics: Lesson 012 - Experiment 13: barbecue LED

Remove headers from some pages in a word document

Self made ramp, but it really smells good

417 sequence traversal of binary tree 1 (102. sequence traversal of binary tree, 107. level traversal of binary tree II, 199. right view of binary tree, 637. layer average of binary tree)

Use Adobe Acrobat pro to resize PDF pages

Debugging mipi-dsi screen based on stm32mp157

初体验完全托管型图数据库 Amazon Neptune

TCP acceleration notes
随机推荐
剑指offer刷题(简单等级)
【补题】2021牛客暑期多校训练营1-3
牛客:飞行路线(分层图+最短路)
Biweekly investment and financial report: capital ambush Web3 infrastructure
allgero报错:Program has encountered a problem and must exit. The design will be saved as a .SAV file
图像超分综述:超长文一网打尽图像超分的前世今生 (附核心代码)
STM32CubeMX 學習(5)輸入捕獲實驗
Websocket understanding and application scenarios
[Mobius inversion]
C disk drives, folders and file operations
什么是SKU和SPU,SKU,SPU的区别是什么
2022年毕业生求职找工作青睐哪个行业?
Luogu p2048 [noi2010] super Piano (rmq+ priority queue)
【补题】2021牛客暑期多校训练营6-n
C # set up FTP server and realize file uploading and downloading
Socket problem record
CVPR 2022 Oral 2D图像秒变逼真3D物体
c#磁盘驱动器及文件夹还有文件类的操作
Deep learning series 48:deepfaker
TCP MIN_RTO 辩证考
