当前位置:网站首页>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;
}
};
边栏推荐
- 企业全面云化的时代——云数据库的未来
- Debugging mipi-dsi screen based on stm32mp157
- How to create a new branch with SVN
- 现在通过开户经理发的开户链接股票开户安全吗?
- What problems do you worry about when you want to switch to software testing?
- Pycharm的奇葩设定:取消注释后立马复制会带上#
- rosbag
- PHP array function Encyclopedia
- [unexpected token o in JSON at position 1 causes and solutions]
- Rosparam statement
猜你喜欢

Apache CouchDB Code Execution Vulnerability (cve-2022-24706) batch POC

五分钟快速搭建一个实时人脸口罩检测系统(OpenCV+PaddleHub 含源码)

How to calculate the D value and W value of statistics in normality test?

Stm32cubemx Learning (5) Input capture Experiment

How to calculate the fuzzy comprehensive evaluation index? How to calculate the four fuzzy operators?

唐老师讲运算放大器(第七讲)——运放的应用

The era of enterprise full cloud -- the future of cloud database

Use Adobe Acrobat pro to resize PDF pages

以科技赋能设计之美,vivo携手知名美院打造“产学研”计划

C examples of using colordialog to change text color and fontdialog to change text font
随机推荐
2022年毕业生求职找工作青睐哪个行业?
Electronics: Lesson 009 - Experiment 7: study relays
面试前准备好这些,Offer拿到手软,将军不打无准备的仗
4个不可不知的采用“安全左移”的理由
Log in to MySQL 5.7 under ubuntu18 and set the root password
[supplementary question] 2021 Niuke summer multi school training camp 1-3
Stm32cubemx learning (5) input capture experiment
打新债的安全性 有风险吗
Static web server
Find out the possible memory leaks caused by the handler and the solutions
What problems do you worry about when you want to switch to software testing?
Logu P2486 [sdoi2011] coloring (tree chain + segment tree + merging of intervals on the tree)
堆栈认知——栈溢出实例(ret2libc)
Overview of image super score: the past and present life of image super score in a single screen (with core code)
[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
Functions should not specify operation types through variables
Luogu p5994 [pa2014]kuglarz (XOR thinking +mst)
How to do factor analysis? Why should data be standardized?
How do I install the software using the apt get command?
