LeetCode_13. Roman to Integer
13. Roman to Integer
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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(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.
package leetcode.easy;
import java.util.HashMap;
public class RomanToInteger {
@org.junit.Test
public void test() {
String s1 = "III";
String s2 = "IV";
String s3 = "IX";
String s4 = "LVIII";
String s5 = "MCMXCIV";
System.out.println(romanToInt(s1));
System.out.println(romanToInt(s2));
System.out.println(romanToInt(s3));
System.out.println(romanToInt(s4));
System.out.println(romanToInt(s5));
}
public int romanToInt(String s) {
if (null == s || 0 == s.length()) {
return -1;
}
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int result = 0;
result += map.get(s.charAt(s.length() - 1));
for (int i = s.length() - 2; i >= 0; i--) {
if (map.get(s.charAt(i)) >= map.get(s.charAt(i + 1))) {
result += map.get(s.charAt(i));
} else {
result -= map.get(s.charAt(i));
}
}
return result;
}
}
LeetCode_13. Roman to Integer的更多相关文章
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 5.Integer to Roman && Roman to Integer
Roman chart: http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm Integer to Roman Given an inte ...
- LeetCode:Roman to Integer,Integer to Roman
首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...
- 58. 分析、测试与总结:罗马数字和阿拉伯数字的转换[roman to integer and integer to roman in c++]
[本文链接] http://www.cnblogs.com/hellogiser/p/roman-to-integer-and-integer-to-roman.html [题目] 给出一个罗马数字, ...
- No.013 Roman to Integer
13. Roman to Integer Total Accepted: 95998 Total Submissions: 234087 Difficulty: Easy Given a roman ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- 3月3日(5) Roman to Integer
原题 Roman to Integer 题意很简单,把Roman字母翻译成int. 实现方式也不难,针对每个字符转成int,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...
随机推荐
- FreeRTOS编程风格
数据类型 基本使用的是标准C里面的数据类型,但是针对不同的处理器,对标准C的数据类型又进行了重定义: 在FreeRTOS中详细的数据类型重定义在portmacro.h这个文件中,具体如下: /* Ty ...
- YOLO---Darknet下的 GPU vs CPU 速度
YOLO---Darknet下的 GPU vs CPU 速度 目录 一.基础环境 二.安装Darknet-yolo v3 三.CPU下测试 四.GPU下测试 五.测试速度对比结论 正文 一.基础环境 ...
- java中的io流总结(二)——RandomAccessFile类
知识点:RandomAccessFile (随机访问文件)类 (一)描述 前一篇博客中https://www.cnblogs.com/shuaifing/p/11490160.html,主要描述Fil ...
- java中的io流总结(一)
知识点:基于抽象基类字节流(InputStream和OutputStream).字符流(Reader和Writer)的特性,处理纯文本文件,优先考虑使用字符流BufferedReader/Buffer ...
- webpack4.0打包优化策略整理小结
本文转载于:https://www.jb51.net/article/137449.htm 介绍了webpack4.0打包优化策略整理小结,分享给大家,具体如下: webapck4 新特性介绍-参考资 ...
- hbase实践之flush and compaction
本文主要涉及flush流程,探讨flush流程过程中引入的问题并阐述2种解决策略,最后简要说明Flush执行策略. 对于Compaction,本文主要探讨Compaction要解决的本质问题以及由Co ...
- github下载慢的问题
1. 修改HOSTS文件:在“C:\Windows\System32\drivers\etc” 下的HOSTS文件,添加以下地址: 151.101.44.249 github.global.ssl. ...
- java后台表单字符串长度验证
1.方法封装前提实体类属性去除首尾空白并进行反转义 //实体类属性的get方法 public String getContent() { if(!"".equals(字符串属性名) ...
- JS客户端类型的判断
**第一种:通过判断浏览器的userAgent,用正则来判断是否是ios和Android客户端.代码如下:** <script type="text/javascript"& ...
- 关于pageHelper无法查到总数踩到的坑
问题代码 PageHelper.startPage(pageNum,pageSize); List<pojoVo> pojoVo=robotService.getPageList(); P ...