当前位置:网站首页>leetcode. 13 --- Roman numeral to integer
leetcode. 13 --- Roman numeral to integer
2022-06-25 08:20:00 【_ End, broken chord】
Roman numeral to integer
12 Turn the question upside down , Solve with hash table . Store Roman numerals and their corresponding values in a hash table , Traverse Roman numerals , If the Roman numeral of the current position is smaller than the one following it, you need to subtract the current Roman numeral , Otherwise, add the current Roman numerals .
The code is as follows :
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;
}
};
边栏推荐
- Overview of image super score: the past and present life of image super score in a single screen (with core code)
- Use pytorch to build mobilenetv2 and learn and train based on migration
- Number theory template
- Opencv minimum filtering (not limited to images)
- Functions should not specify operation types through variables
- STM32CubeMX 学习(5)输入捕获实验
- A solution to slow startup of Anaconda navigator
- [supplementary question] 2021 Niuke summer multi school training camp 6-n
- Daily question brushing record (III)
- 什么是SKU和SPU,SKU,SPU的区别是什么
猜你喜欢
In 2022, which industry will graduates prefer when looking for jobs?
面试前准备好这些,Offer拿到手软,将军不打无准备的仗
Stm32cubemx learning (5) input capture experiment
测一测现在的温度
使用apt-get命令如何安装软件?
Sword finger offer (medium level)
初体验完全托管型图数据库 Amazon Neptune
Find out the possible memory leaks caused by the handler and the solutions
To understand the difference between Gram-positive and Gram-negative bacteria and the difference in pathogenicity
TCP and UDP
随机推荐
[supplementary question] 2021 Niuke summer multi school training camp 1-3
Drawing of clock dial
函数尽量不要通过变量指定操作类型
C disk drives, folders and file operations
Debugging mipi-dsi screen based on stm32mp157
Pycharm的奇葩设定:取消注释后立马复制会带上#
Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier
CVPR 2022 Oral 2D图像秒变逼真3D物体
Solving some interesting problems with recurrence of function
Electronics: Lesson 014 - Experiment 15: intrusion alarm (Part I)
What is SKU and SPU? What is the difference between SKU and SPU
FM signal, modulated signal and carrier
Common SRV types
TCP acceleration notes
4個不可不知的采用“安全左移”的理由
[supplementary question] 2021 Niuke summer multi school training camp 4-N
想开个户,网上股票开户安不安全?
电子学:第013课——实验 14:可穿戴的脉冲发光体
Sword finger offer (medium level)
Authority design of SaaS system based on RBAC