当前位置:网站首页>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;
}
};
边栏推荐
猜你喜欢

助力极致体验,火山引擎边缘计算最佳实践

小白学习MySQL - 增量统计SQL的需求 - 开窗函数的方案

保障饮用水安全!番禺沙湾水道水质在线监测系统通过验收

bison使用error死循环的记录

How to view saved passwords of websites

杰理之关于 TWS 声道配置【篇】

Numpy's ndarray array Foundation
联想领像 lenovoimage 部分打印机 驱动 PPD 文件

Wonderful! Miaoying technology fully implements Zadig to help container construction, and fully embraces kubernetes and Yunyuan
![Jerry's configuration of TWS cross pairing [chapter]](/img/35/a041093ce3dab8e2739bbc1466ba29.png)
Jerry's configuration of TWS cross pairing [chapter]
随机推荐
Pytorch - 分布式通信原语(附源码)
黑化的蜜雪冰城,凭营销就想抓牢消费者的心?
二叉树递归与迭代
LM07丨细聊期货横截面策略
跟着官方学电机,BLDC两种控制策略,学到即赚到
请问股票开户收费吗 网上开户安全吗?
2022 Guangxi provincial safety officer C certificate examination questions and mock examination
&4 express框架
力扣每日一题-第31天-13.罗马数组转整数
Jerry's manual pairing method [chapter]
ShanDong Multi-University Training #3
TTChat x Zadig 开源共创 Helm 接入场景,环境治理搞得定!
杰理之关于开机发起回连对耳的位置:【篇】
这个EMR-SparkSQL节点,他查询的表是不是ODPS的啊?
【VTK】MFC基于VTK8.2的网格编辑器
Oracle NetSuite 助力 TCM Bio,洞悉数据变化,让业务发展更灵活
MariaDB的安装与配置
稳定币风险状况:USDT 和 USDC 安全吗?
Numpy的ndarray数组基础
Is the table queried by this EMR sparksql node ODPs?