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:
I
can be placed beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can 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,从右往左,依次判断,如果当前值比上一个值大则相加,小则相减. 什么,你问我怎 ...
随机推荐
- C#中[STAThread]的作用
[STAThread]STAThread:Single Thread Apartment Thread.(单一线程单元线程)[]是用来表示Attributes: [STAThread]是一种线程模型, ...
- c线程使用锁控制并发
// // Created by gxf on 2019/12/16. // #include <stdlib.h> #include <stdio.h> #include & ...
- 标准键盘 acsii码值 表
码值 含义 备注 0x08 Backspace键 0x09 Tab键 0x0C Clear键 Num Lock关闭时的数字键盘5 0x0D Enter键 0x10 Shift键 0x1 ...
- 深度解析Graph Embedding
Graph Embedding是推荐系统.计算广告领域最近非常流行的做法,是从word2vec等一路发展而来的Embedding技术的最新延伸:并且已经有很多大厂将Graph Embedding应用于 ...
- PHP 获取上传文件的实际类型
方案一: mime_content_type ( string $filename ) : string (PHP 4 >= 4.3.0, PHP 5, PHP 7) mime_content_ ...
- 第69题:x的平方根
一. 问题描述 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输 ...
- docker压缩导入导出
导出镜像 docker save <myimage>:<tag> | gzip > <myimage>_<tag>.tar.gz 导入镜像 gun ...
- 详解Python 切片语法
Python的切片是特别常用的功能,主要用于对列表的元素取值.这篇文章主要介绍了详解Python 切片语法,需要的朋友可以参考下 Python的切片是特别常用的功能,主要用于对列表的元素取值.使用切片 ...
- LOJ2265. 「CTSC2017」最长上升子序列
题意:中文题意很清楚 LOJ2263 分析: 根据Dilworth定理,最小链覆盖=最长反链. 问题转化为求 $k$ 个最小不上升序列能覆盖的最大数的个数. 参考链接: 1. https://blog ...
- [Google Guava] 9-I/O
原文链接 译文链接 译者:沈义扬 字节流和字符流 Guava使用术语”流” 来表示可关闭的,并且在底层资源中有位置状态的I/O数据流.术语”字节流”指的是InputStream或OutputStrea ...