当前位置:网站首页>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;
}
};
边栏推荐
- A solution to slow startup of Anaconda navigator
- Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
- 飞机引气系统的建模与故障仿真
- [supplementary question] 2021 Niuke summer multi school training camp 9-N
- [QT] qtcreator shortcut key and QML introduction
- 面试前准备好这些,Offer拿到手软,将军不打无准备的仗
- [Mobius inversion]
- Remove headers from some pages in a word document
- How do I install the software using the apt get command?
- What is the difference between agreement and service?
猜你喜欢

FM signal, modulated signal and carrier

TCP 加速小记

Data-centric vs. Model-centric. The Answer is Clear!

电子学:第012课——实验 11:光和声

Daily question brushing record (III)

Deep learning series 48:deepfaker

电子学:第008课——实验 6:非常简单的开关

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

TCP stuff

C # set up FTP server and realize file uploading and downloading
随机推荐
以科技赋能设计之美,vivo携手知名美院打造“产学研”计划
STM32CubeMX 學習(5)輸入捕獲實驗
How to do factor analysis? Why should data be standardized?
使用apt-get命令如何安装软件?
Sword finger offer (medium level)
想开个户,网上股票开户安不安全?
Stm32cubemx learning (5) input capture experiment
电子学:第012课——实验 11:光和声
现在通过开户经理发的开户链接股票开户安全吗?
图像超分综述:超长文一网打尽图像超分的前世今生 (附核心代码)
电子学:第008课——实验 6:非常简单的开关
Almost taken away by this wave of handler interview cannons~
leetcode.13 --- 罗马数字转整数
Use pytorch to build mobilenetv2 and learn and train based on migration
Luogu p2048 [noi2010] super Piano (rmq+ priority queue)
PH neutralization process modeling
TCP acceleration notes
C # set up FTP server and realize file uploading and downloading
VOCALOID notes
电子学:第010课——实验 8:继电振荡器
