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 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相关推荐
- LeetCode 13. Roman to Integer
问题链接 LeetCode 13. Roman to Integer 题目解析 将罗马数字转换成普通数字. 解题思路 先简单了解一下什么是罗马数字. 基本字符:I,V,X,L,C,D,M 相应的阿拉伯 ...
- leetcode python3 简单题13. Roman to Integer
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第十三题 (1)题目 英文: Given a roman numeral, conv ...
- LeetCode: 13. Roman to Integer
051106 题目 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol ...
- 【LeetCode】13. Roman to Integer
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 13. Roman to Integer
1.问题描述 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Val ...
- 13. Roman to Integer
2019独角兽企业重金招聘Python工程师标准>>> 见12 转载于:https://my.oschina.net/datacube/blog/702856
- 13. Roman to Integer
解题思路: 1)将所有字母转化为对应的数值: 2)如果字符串长度等于1,直接输出这一个字母对应的数值:如果大于1,则比较前后两个字母对应数值的大小,如果前面小于后面,则后面的减去前面在叠加到总和上,反 ...
- leetcode 12 ,13 Integer to Roman amp;amp;Roman to Integer 罗马与阿拉伯数组转换
12 Integer to Roman 13 Roman to Integer 有可能不注意的结果: class Solution {public:/*1.相同的数字连写,所表示的数等于这些数字相加得 ...
- LeetCode刷题实战(13):Roman to Integer
题目描述: 13 Roman to Integer 49.5% Easy Roman numerals are represented by seven different symbols: I, ...
- 罗马数字转整数 · Roman to Integer
13. Roman to Integer [抄题]: [暴力解法]: 时间分析: 空间分析: [思维问题]: 没有想到罗马字是逆序的情况 没有想到要先用toCharArray()方法把字符串拆成一个字 ...
最新文章
- Homebrew存在大漏洞,恶意代码远程操纵电脑! 网友:这不是单方面的责任
- markdown 创建表格
- matlab simulink 直线一级倒立摆控制方法研究 状态观测
- 项目: 打字母游戏图形化【C++ / C】
- 15条最重要且最基本的SEO优化Tips和技巧
- 嵌入式文件系统镜像制作及烧写
- mysql数据库生成json_Python3实现 将MySQL数据库中的记录生成JSON数据
- 图书管理模块功能设计
- ELK学习总结(2-5)elk的版本控制
- C++中不能声明为虚函数的有哪些函数
- 2. MarkText可代替Typora的markdown 编辑器
- 链家网页爬虫_R爬虫小白入门:Rvest爬链家网+分析(一)
- gin -get请求的小示例1-Handle处理GET请求
- windows下编译可在visual studio中调试的FFmpeg
- 微信小程序引入外部icon
- 碳足迹PCF国内外标准和碳标签应用
- python之arp欺骗
- 查看exe和dll等二进制文件时间戳(生成时间)的工具与方法介绍
- Linux常用浏览器
- 有关PHP文档生成工具---PHPDocumentor
热门文章
- MediaFire – 美国无限容量免费网络硬盘
- OSChina 周三乱弹 —— 请叫我马化喵
- vc++6.0和visual studio 2005
- 【求职】陌陌 Java 方向面经
- C语言 这天星期几?
- c 将mysql表数据打印,c 中数据库数据如何导出至excel表格-用sql语句把一个数据库内的表保存成excel表格??...
- AES加密算法 Java与Python跨平台实现
- elementUIel-input和el-select宽度不一样
- 浅浅轻吟如花的芬芳,开始散漫着
- 小米彷徨:股价与业绩的自我救赎