当前位置:网站首页>Li Kou daily question - day 31 -13 Roman array to integer
Li Kou daily question - day 31 -13 Roman array to integer
2022-06-29 12:10:00 【Chongyou research Sen】
2022.6.29 Did you brush the questions today ?
subject :
Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
character The number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
for example , Rome digital 2 Write to do II , Two parallel 1 .12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
C Can be placed in D (500) and M (1000) Left side , To express 400 and 900.
Given a Roman number , Convert it to an integer .
analysis :
Give you a string , You need to convert it into numbers according to Roman rules , What needs to be noted is
The front number Less than Back , Make a difference
Front array Greater than Back , Then do and
Ideas : We insert the relationship between strings and numbers into the hash table , Then iterate through the string , Judge according to the mapping relationship of hash table , Store the value in vector in , Right again vector Make a judgment and get the result
analysis :
1. Hash table solving
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int>map;
vector<int>vec;
int res = 0;
map['I'] = 1;
map['V'] = 5;
map['X'] = 10;
map['L'] = 50;
map['C'] = 100;
map['D'] = 500;
map['M'] = 1000;
for (auto& c : s)
{
vec.push_back(map[c]);
}
for (auto i = 0; i < vec.size();i++ )
{
if (i < vec.size() - 1 && vec[i] < vec[i + 1])
{
res -= vec[i];
}
else
{
res += vec[i];
}
}
return res;
}
};2. Hash table optimization
We can vector Omit , Directly use the mapping relationship to make judgments
class Solution {
public:
int romanToInt(string s) {
unordered_map<char, int>map;
int res = 0;
map['I'] = 1;
map['V'] = 5;
map['X'] = 10;
map['L'] = 50;
map['C'] = 100;
map['D'] = 500;
map['M'] = 1000;
for (auto i = 0; i < s.size(); i++)
{
if (i < s.size() - 1 && map[s[i]] < map[s[i+1]])
{
res -= map[s[i]];
}
else
{
res += map[s[i]];
}
}
return res;
}
};
边栏推荐
- ETL为什么经常变成ELT甚至LET?
- Jerry's configuration of TWS cross pairing [chapter]
- 记一次 MSI 笔记本 GE63 播放网页视频 闪屏和随机发绿 问题解决
- 黑化的蜜雪冰城,凭营销就想抓牢消费者的心?
- Take another picture of cloud redis' improvement path
- Sofaregistry source code | data synchronization module analysis
- 将文件夹打包成iso
- MariaDB的安装与配置
- Equals increases execution speed / performance optimization
- MATLAB Gui 实现点击按钮,打开文件对话框,导入图片功能
猜你喜欢

& 3 view request message and response message in browser

Numpy的ndarray数组基础

Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活

Rebuild confidence in China's scientific research - the latest nature index 2022 released that China's research output increased the most
![Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cycle functions [chapter]](/img/d7/f73e748ada302440326a8b1a46f916.png)
Jerry's initiation of ear pairing, reconnection, and opening of discoverable and connectable cycle functions [chapter]

ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?

MATLAB Gui 实现点击按钮,打开文件对话框,导入图片功能

Helping the ultimate experience, best practice of volcano engine edge computing

易快报:我们用 Zadig 实现万次构建部署,聪明运维,释放开发生产力
![[pbootcms模板]作文网站/文档下载网站源码](/img/6e/51bbb4ce961defa4abd098ff3af21f.jpg)
[pbootcms模板]作文网站/文档下载网站源码
随机推荐
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
Is the table queried by this EMR sparksql node ODPs?
检查YAML文件安全配置:kubesec
Easy express: we use Zadig to realize 10000 times of construction and deployment, smart operation and maintenance, and release development productivity
Pytorch - 分布式通信原语(附源码)
保障饮用水安全!番禺沙湾水道水质在线监测系统通过验收
Oracle expands distributed cloud services to bring comprehensive public cloud services to more customers
2022胺基化工艺考试题模拟考试题库及在线模拟考试
Jerry's about TWS pairing mode configuration [chapter]
Installation and configuration of MariaDB
& 4 express framework
龙书虎书鲸书啃不动?试试豆瓣评分9.5的猴书
RSLO:自监督激光雷达里程计(实时+高精度,ICRA2022)
杰理之关于开机发起回连对耳的位置:【篇】
CICD简介[通俗易懂]
在校生的编程故事
【文献翻译】Concealed Object Detection(伪装目标检测)
LM07丨细聊期货横截面策略
如何通过WinDbg获取方法参数值
启泰观察:职业精英创业必踩巨坑之 --- 学习效果坑