当前位置: 首页 > news >正文

闯关leetcode——13. Roman to Integer

大纲

  • 题目
    • 地址
    • 内容
  • 解题
    • 代码地址

题目

地址

https://leetcode.com/problems/roman-to-integer/description/

内容

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

SymbolValue
I1
V5
X10
L50
C100
D500
M1000

For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.
  • Given a roman numeral, convert it to an integer.

Example 1:

Input: s = “III”
Output: 3
Explanation: III = 3.

Example 2:

Input: s = “LVIII”
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 3:

Input: s = “MCMXCIV”
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

Constraints:

  • 1 <= s.length <= 15
  • s contains only the characters (‘I’, ‘V’, ‘X’, ‘L’, ‘C’, ‘D’, ‘M’).
  • It is guaranteed that s is a valid roman numeral in the range [1, 3999].

解题

这题是要求将罗马数字转换成整型数字。这就要熟悉罗马数字特点:

  • 罗马数字通过较小的数字在较大的数字的左侧还是右侧,来决定较小的数字是进行加法操作还是减法操作。如果较小数字出现在较大数字左侧,则表示减去较小数字。比如IV,V表示5,I表示1,I小于V且出现在V的左侧,则IV表示5-1=4;如果较小的数字出现在较大数字的右侧,则表示加上较大的数字。比如VI,I出现在V的右侧,于是VI表示5+1=4。
  • 在表示一个比较大的数字时,大的数字在左侧。比如1994(MCMXCIV),表示千位的M会最先出现,表示4的IV最后出现。

基于上述规律,我们可以得出结论:如果一个数比上个数字小,则减去该数;如果大于等于上个数字,则加上该数。最后一位一定是加上。

#include <string>
#include <unordered_map>
using namespace std;class Solution {
public:int romanToInt(string s) {const unordered_map<char, int> roman_map = {{'I', 1},{'V', 5},{'X', 10},{'L', 50},{'C', 100},{'D', 500},{'M', 1000}};int result = 0;int previous_value = 0;for (char c : s) {int current_value = roman_map.at(c);if (previous_value < current_value) {result -= previous_value;} else {result += previous_value;}previous_value = current_value;}result += previous_value;return result;}
};

代码地址

https://github.com/f304646673/leetcode/tree/main/13-Roman-to-Integer

在这里插入图片描述

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • python从入门到精通:面向对象
  • 练习:基于TCP协议实现一个网盘系统
  • 在windows下抓空包(monitor网卡+wareshark+MNM)
  • 105页PPT:华为ISC集成供应链变革、模式与方法解析
  • QT如何ui上的QTableWidget控件如何使用
  • 误删文件后的数据救赎实战恢复指南
  • ABB HDS大功率永磁同步伺服电机的创新特性
  • Java stream使用与执行原理
  • 速盾:你知道高防 IP 和高防 CDN 的区别吗?
  • Golang | Leetcode Golang题解之第395题至少有K个重复字符的最长子串
  • 《Python读取 Excel 数据》
  • 美术|基于java+vue的美术外包管理信息系统(源码+数据库+文档)
  • Java数据库连接池的优化与配置
  • 音视频入门基础:AAC专题(1)——AAC官方文档下载
  • Vue(7)——工程化开发
  • [LeetCode] Wiggle Sort
  • [nginx文档翻译系列] 控制nginx
  • 10个确保微服务与容器安全的最佳实践
  • 3.7、@ResponseBody 和 @RestController
  • Android框架之Volley
  • CentOS6 编译安装 redis-3.2.3
  • java8-模拟hadoop
  • jquery ajax学习笔记
  • Linux链接文件
  • magento2项目上线注意事项
  • oschina
  • PHP 的 SAPI 是个什么东西
  • SpiderData 2019年2月25日 DApp数据排行榜
  • Webpack4 学习笔记 - 01:webpack的安装和简单配置
  • XForms - 更强大的Form
  • 持续集成与持续部署宝典Part 2:创建持续集成流水线
  • 第十八天-企业应用架构模式-基本模式
  • 对话 CTO〡听神策数据 CTO 曹犟描绘数据分析行业的无限可能
  • 记一次用 NodeJs 实现模拟登录的思路
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 猫头鹰的深夜翻译:JDK9 NotNullOrElse方法
  • 如何编写一个可升级的智能合约
  • 如何选择开源的机器学习框架?
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • mysql 慢查询分析工具:pt-query-digest 在mac 上的安装使用 ...
  • #我与Java虚拟机的故事#连载17:我的Java技术水平有了一个本质的提升
  • (39)STM32——FLASH闪存
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (八)c52学习之旅-中断实验
  • (小白学Java)Java简介和基本配置
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转载)(官方)UE4--图像编程----着色器开发
  • (转载)PyTorch代码规范最佳实践和样式指南
  • .NET BackgroundWorker
  • .NET Core、DNX、DNU、DNVM、MVC6学习资料
  • .net 开发怎么实现前后端分离_前后端分离:分离式开发和一体式发布
  • .Net8 Blazor 尝鲜
  • .NET开源全面方便的第三方登录组件集合 - MrHuo.OAuth
  • .NET企业级应用架构设计系列之技术选型
  • .NET未来路在何方?