051106

题目

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

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven 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. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

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

题意

有7个这样的罗马字母,分别代表7个数字。一般来说,写法是从大到小写为从左到右的,然后依次相加。但是为了更简洁的表示一些大值,当写法为从小到大时,则表示相减。

我的解题思路

首先建立一个字符到数值的map,然后判断它们的位置和大小,如果大的在前就直接加大的,如果小的在前,则加(大值-小值),注意对最后一个字符的处理。

class Solution {
public:int romanToInt(string s) {unordered_map<char, int> map(7);map['I']=1;map['V']=5;map['X']=10;map['L']=50;map['C']=100;map['D']=500;map['M']=1000;if(s.length()==1) return map[s[0]];int res=0;for(int i=0; i<s.length()-1; i++){if(map[s[i]]>=map[s[i+1]]) {res+=map[s[i]];}else{res+=(map[s[i+1]]-map[s[i]]);i++; }if(i==s.length()-2) res+=map[s[i+1]];}return res;     }
};

看了下高赞解答,发现思路跟我也差不多。

LeetCode: 13. Roman to Integer相关推荐

  1. LeetCode 13. Roman to Integer

    问题链接 LeetCode 13. Roman to Integer 题目解析 将罗马数字转换成普通数字. 解题思路 先简单了解一下什么是罗马数字. 基本字符:I,V,X,L,C,D,M 相应的阿拉伯 ...

  2. leetcode 8 Roman to Integer C++实现

    leetcode 8 Roman to Integer C++实现 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D ...

  3. [LeetCode][JavaScript]Roman to Integer

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  4. leetcode python3 简单题13. Roman to Integer

    1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十三题 (1)题目 英文: Given a roman numeral, conv ...

  5. 【LeetCode】13. Roman to Integer

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  6. 【leetcode】Roman to Integer

    题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  7. 13. Roman to Integer

    1.问题描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Val ...

  8. 13.  Roman to Integer

    2019独角兽企业重金招聘Python工程师标准>>> 见12 转载于:https://my.oschina.net/datacube/blog/702856

  9. 13. Roman to Integer

    解题思路: 1)将所有字母转化为对应的数值: 2)如果字符串长度等于1,直接输出这一个字母对应的数值:如果大于1,则比较前后两个字母对应数值的大小,如果前面小于后面,则后面的减去前面在叠加到总和上,反 ...

最新文章

  1. Greenplum——升级的分布式PostgresSQL
  2. git http仓库账号密码缓存至本地:不用每次git push都需要输入密码的方法(类似于windows的凭据管理)
  3. php 模块 远程,ThinkPHP远程调用模块的操作方法 URL 参数格式
  4. SQL error: cannot use the special principal 'sa'
  5. 大家身边极度聪明的人是什么样子?
  6. 字符串转小写 c语言库函数,c++字符串大小写转换
  7. 程序员面试金典——18.9实时中位数
  8. 推荐一款UI非常Good的 Redis 客户端工具
  9. html格式动画怎么导入ppt,PPT导入/导出
  10. 学习SQL Server这一篇就够了
  11. java从入门到精通----mysql05
  12. 浮点数的二进制表示方法
  13. TM1637带秒点四位LED显示器模块ARDUINO驱动程序
  14. 乳腺肿瘤超声图像感兴趣区域的自动识别综述
  15. O(n)的时间复杂度求中位数
  16. 面试宝典:破解最难回答的23个问题及8大面试通关考题
  17. Cookie-网站登录-下次自动登录
  18. php 小时,php - 将秒转换为小时:分钟:秒
  19. 用matlab作地震波vsp图,用于深井VSP地震波检测的光纤配重装置的制作方法
  20. 机器学习算法——线性回归的详细介绍 及 利用sklearn包实现线性回归模型

热门文章

  1. CentOs7安装tomcat
  2. [转]C++ 使用Makefile文件
  3. windows中路径\和 linux中用/
  4. 手把手pytorch-transformers实战
  5. 自然语言处理工具类数地工厂
  6. 用图解释RNN运行的部分过程
  7. 智源青年科学家林乾:揭开人工智能的黑匣,从解答最基本的问题开始
  8. 字节跳动:基于H.266/VVC的移动平台8K超高清实时解码实践 | QCon
  9. 为什么 Linux 和 macOS 不需要碎片整理
  10. 学数学,你要如何过题海:游泳?冲浪?划小船?开游艇?