当前位置:网站首页>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;
}
};
边栏推荐
- 电子学:第012课——实验 11:光和声
- Electronics: Lesson 010 - Experiment 8: relay oscillator
- Pycharm的奇葩设定:取消注释后立马复制会带上#
- Network model -- OSI model and tcp/ip model
- 电子学:第011课——实验 10:晶体管开关
- Solving some interesting problems with recurrence of function
- 测一测现在的温度
- Authority design of SaaS system based on RBAC
- Electronics: Lesson 012 - Experiment 11: light and sound
- Biweekly investment and financial report: capital ambush Web3 infrastructure
猜你喜欢

Socket problem record

Solving some interesting problems with recurrence of function

A solution to slow startup of Anaconda navigator

自制坡道,可是真的很香

Static web server

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

Electronics: Lesson 012 - Experiment 11: light and sound

CVPR 2022 Oral 2D图像秒变逼真3D物体

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

物联网毕设(智能灌溉系统 -- Android端)
随机推荐
函数尽量不要通过变量指定操作类型
How to create a new branch with SVN
Common action types
What problems do you worry about when you want to switch to software testing?
LeetCode_ Hash table_ Medium_ 454. adding four numbers II
Authority design of SaaS system based on RBAC
使用pytorch搭建MobileNetV2并基于迁移学习训练
Electronics: Lesson 012 - Experiment 13: barbecue LED
共话云原生数据库的未来
Opencv minimum filtering (not limited to images)
FM signal, modulated signal and carrier
STM32CubeMX 學習(5)輸入捕獲實驗
Remove headers from some pages in a word document
Electronics: Lesson 013 - Experiment 14: Wearable pulsed luminaries
Ffmpeg+sdl2 for audio playback
Determine whether the user is entering a page for the first time
Mr. Tang's lecture on operational amplifier (Lecture 7) -- Application of operational amplifier
Electronics: Lesson 011 - experiment 10: transistor switches
Niuke: flight route (layered map + shortest path)
[thesis study] vqmivc
