题目描述:

Roman numerals are represented by seven different symbols: IVXLCD 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.

代码解答:

package com.jack.algorithm;import java.util.HashMap;
import java.util.Map;/*** create by jack 2018/10/27** @author jack* @date: 2018/10/27 08:20* @Description:* 罗马数字转换为整数*/
public class RomanToInteger {/*** 题目描述:* https://leetcode.com/problems/roman-to-integer/*** @param s* @return*/public static int romanToInt(String s) {//为空则直接返回0if (s.isEmpty()) {return 0;}int length = s.length();int sum = 0;Map<Character, Integer> charIntMap = getCharInt();//如果为1,则直接获取if (length == 1) {sum += charIntMap.get(s.charAt(0));}//如果大于1,长度为2以上if (length > 1) {//4 or 9if (s.charAt(0) == 'I'&& (s.charAt(1) == 'V' ||s.charAt(1) == 'X')) {sum += charIntMap.get(s.charAt(1)) - charIntMap.get(s.charAt(0));if (length > 2) {sum +=romanToInt(s.substring(2));}//40 or 90} else if (s.charAt(0) == 'X' &&(s.charAt(1) == 'L' ||s.charAt(1) == 'C')) {sum += charIntMap.get(s.charAt(1)) - charIntMap.get(s.charAt(0));if (length > 2) {sum +=romanToInt(s.substring(2));}//400 or 900}else if (s.charAt(0) == 'C' &&(s.charAt(1) == 'D' ||s.charAt(1) == 'M')) {sum += charIntMap.get(s.charAt(1)) - charIntMap.get(s.charAt(0));if (length > 2) {sum +=romanToInt(s.substring(2));}} else {sum += charIntMap.get(s.charAt(0));sum += romanToInt(s.substring(1));}}return sum;}public static Map<Character, Integer> getCharInt(){Map<Character, Integer> intCharMap = new HashMap<Character, Integer>(16);intCharMap.put('I', 1);intCharMap.put('V', 5);intCharMap.put('X', 10);intCharMap.put('L', 50);intCharMap.put('C', 100);intCharMap.put('D', 500);intCharMap.put('M', 1000);return intCharMap;}public static void main(String[] args) {//int sum = romanToInt("III");//int sum = romanToInt("IV");//int sum = romanToInt("IX");//int sum = romanToInt("LVIII");int sum = romanToInt("DCXXI");System.out.println("sum="+sum);}
}

源代码地址:

源码

13-roman-to-integer相关推荐

  1. LeetCode 13. Roman to Integer

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

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

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

  3. LeetCode: 13. Roman to Integer

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

  4. 【LeetCode】13. Roman to Integer

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

  5. 13. Roman to Integer

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

  6. 13.  Roman to Integer

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

  7. 13. Roman to Integer

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

  8. leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换

    12 Integer to Roman 13 Roman to Integer 有可能不注意的结果: class Solution {public:/*1.相同的数字连写,所表示的数等于这些数字相加得 ...

  9. LeetCode刷题实战(13):Roman to Integer

    题目描述: 13 Roman to Integer   49.5% Easy Roman numerals are represented by seven different symbols: I, ...

  10. 罗马数字转整数 · Roman to Integer

    13. Roman to Integer [抄题]: [暴力解法]: 时间分析: 空间分析: [思维问题]: 没有想到罗马字是逆序的情况 没有想到要先用toCharArray()方法把字符串拆成一个字 ...

最新文章

  1. Homebrew存在大漏洞,恶意代码远程操纵电脑! 网友:这不是单方面的责任
  2. markdown 创建表格
  3. matlab simulink 直线一级倒立摆控制方法研究 状态观测
  4. 项目: 打字母游戏图形化【C++ / C】
  5. 15条最重要且最基本的SEO优化Tips和技巧
  6. 嵌入式文件系统镜像制作及烧写
  7. mysql数据库生成json_Python3实现 将MySQL数据库中的记录生成JSON数据
  8. 图书管理模块功能设计
  9. ELK学习总结(2-5)elk的版本控制
  10. C++中不能声明为虚函数的有哪些函数
  11. 2. MarkText可代替Typora的markdown 编辑器
  12. 链家网页爬虫_R爬虫小白入门:Rvest爬链家网+分析(一)
  13. gin -get请求的小示例1-Handle处理GET请求
  14. windows下编译可在visual studio中调试的FFmpeg
  15. 微信小程序引入外部icon
  16. 碳足迹PCF国内外标准和碳标签应用
  17. python之arp欺骗
  18. 查看exe和dll等二进制文件时间戳(生成时间)的工具与方法介绍
  19. Linux常用浏览器
  20. 有关PHP文档生成工具---PHPDocumentor

热门文章

  1. MediaFire – 美国无限容量免费网络硬盘
  2. OSChina 周三乱弹 —— 请叫我马化喵
  3. vc++6.0和visual studio 2005
  4. 【求职】陌陌 Java 方向面经
  5. C语言 这天星期几?
  6. c 将mysql表数据打印,c 中数据库数据如何导出至excel表格-用sql语句把一个数据库内的表保存成excel表格??...
  7. AES加密算法 Java与Python跨平台实现
  8. elementUIel-input和el-select宽度不一样
  9. 浅浅轻吟如花的芬芳,开始散漫着
  10. 小米彷徨:股价与业绩的自我救赎