当前位置:网站首页>力扣每日一题-第31天-13.罗马数组转整数
力扣每日一题-第31天-13.罗马数组转整数
2022-06-29 11:18:00 【重邮研究森】
2022.6.29今天你刷题了吗?
题目:
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。
分析:
给你一个字符串,你需要按照罗马规则把它转为数字,其中需要注意的是
前面数字 小于 后面,则做差
前面数组 大于 后面,则做和
思路:我们把对于字符串和数字关系插入哈希表中,然后遍历字符串,根据哈希表映射关系进行判断,把值存入vector中,再对vector进行判断得到结果
解析:
1.哈希表求解
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.哈希表优化
我们可以把vector省去,直接利用映射关系做判断
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;
}
};
边栏推荐
- Oracle expands distributed cloud services to bring comprehensive public cloud services to more customers
- &4 express框架
- 【高并发】缓存思路
- 杰理之关于 TWS 声道配置【篇】
- seekg ()[通俗易懂]
- Deep understanding of volatile keyword
- win11网页版
- 记一次 MSI 笔记本 GE63 播放网页视频 闪屏和随机发绿 问题解决
- Jerry's about TWS pairing mode configuration [chapter]
- 恒泰证券开户安全吗 证券排名
猜你喜欢

Qt学习01 GUI程序原理分析

亲测!Centos7部署PHP + Swoole

Information technology application and innovation professionals (database) intermediate training hot enrollment (July 6-10)

Discussion on QT learning 10 message processing in QT

Qt学习02 GUI程序实例分析

Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"

Self-Improvement! Junior college "counter attack" master of Zhejiang University, 3 SCI, and finally become a doctor of Tsinghua University!

Qt学习06 窗口部件及窗口类型

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

Win11 web version
随机推荐
CICD简介[通俗易懂]
Jerry's about TWS pairing mode configuration [chapter]
Follow Me Study HCIE-Big Data-Data Mining 第一章 数据挖掘介绍 模块一
mysql 1146错误[通俗易懂]
Haitai Advanced Technology | application of privacy computing technology in medical data protection
2022 amination process test question simulation test question bank and online simulation test
地平线开发板配置网段
[pbootcms模板]作文网站/文档下载网站源码
又拍云 Redis 的改进之路
&3 在浏览器中查看请求报文和响应报文
Numpy's ndarray array Foundation
Go 单元测试入门实践
RepOptimizer: 其实是RepVGG2
【高并发】2.8万字的Callable和Future面试知识点总结,看完我直接面进了字节跳动,原谅我有点飘了(中)
Safety innovation practice | Haitai Fangyuan was invited to participate in the technical exchange Seminar on "network information innovation and value co creation in the digital age"
2022 Guangxi provincial safety officer C certificate examination questions and mock examination
杰理之关于 TWS 声道配置【篇】
leetcode刷题:字符串07(重复的子字符串)
SOFARegistry 源码|数据同步模块解析
力扣(LeetCode)179. 最大数(2022.06.28)