当前位置:网站首页>leetcode004--罗马数字转整数
leetcode004--罗马数字转整数
2022-04-23 04:38:00 【singularityDZF】
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test04 {
/**
* 13. 罗马数字转整数
* 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
*
* 字符 数值
* I 1
* V 5
* X 10
* L 50
* C 100
* D 500
* M 1000
* 例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
* 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
*
* I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
* X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
* C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
* 给定一个罗马数字,将其转换成整数。
*
*
* 示例 1:
* 输入: s = "III"
* 输出: 3
*
* 示例 2:
* 输入: s = "IV"
* 输出: 4
*
* 示例 3:
* 输入: s = "IX"
* 输出: 9
*
* 示例 4:
* 输入: s = "LVIII"
* 输出: 58
* 解释: L = 50, V= 5, III = 3.
*
* 示例 5:
* 输入: s = "MCMXCIV"
* 输出: 1994
* 解释: M = 1000, CM = 900, XC = 90, IV = 4.
*
* 提示:
* 1 <= s.length <= 15
* s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M')
* 题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内
* 题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。
* IL 和 IM 这样的例子并不符合题目要求,49 应该写作 XLIX,999 应该写作 CMXCIX 。
* 关于罗马数字的详尽书写规则,可以参考 罗马数字 - Mathematics 。
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入罗马数字:");
String romanStr = sc.nextLine();
System.out.println(romanToInt(romanStr));
}
public static int romanToInt(String roamnStr){
Map<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 res = 0;
for(int i=0; i<roamnStr.length(); i++){
if(map.containsKey(roamnStr.charAt(i)) && i<roamnStr.length()-1 && map.get(roamnStr.charAt(i))<map.get(roamnStr.charAt(i+1))){
res -= map.get(roamnStr.charAt(i));
}else{
res += map.get(roamnStr.charAt(i));
}
}
return res;
}
}
版权声明
本文为[singularityDZF]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dangzefei/article/details/124356848
边栏推荐
- Cortex-M3寄存器组、汇编语言与C语言的接口介绍
- Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
- MYSQL50道基础练习题
- [echart] Introduction to echart
- Mysql, binlog log query
- Nature medicine reveals individual risk factors of coronary artery disease
- STM32 MCU ADC rule group multi-channel conversion DMA mode
- 优麒麟 22.04 LTS 版本正式发布 | UKUI 3.1开启全新体验
- Supplément: annotation
- IDE idea automatic compilation and configuration of on update action and on frame deactivation
猜你喜欢
![[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022](/img/eb/916a3fc1a89356fd8e74b943172ac3.png)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022

Chapter 4 - understanding standard equipment documents, filters and pipelines

Coinbase: basic knowledge, facts and statistics about cross chain bridge

QML进阶(四)-绘制自定义控件
![[BIM introduction practice] wall hierarchy and FAQ in Revit](/img/95/e599c7547029f57ce23ef4b87e8b9a.jpg)
[BIM introduction practice] wall hierarchy and FAQ in Revit

520.检测大写字母

Supplement: Annotation
![[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022](/img/b2/269ae2e9be269c2bff73eb1da5b55d.png)
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022

为什么推荐你学嵌入式

Detailed explanation of life cycle component of jetpack
随机推荐
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
[AI vision · quick review of NLP natural language processing papers today, issue 31] Fri, 15 APR 2022
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
智能电子秤全国产化电子元件推荐方案
Redis 命令大全
C language character constant
Error occurs when thymeleaf th: value is null
Why recommend you to study embedded
io.Platform.packageRoot; // ignore: deprecated_member_use
Chapter 4 - understanding standard equipment documents, filters and pipelines
那些年我面试过的Android开发岗总结(附面试题+答案解析)
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
QML进阶(五)-通过粒子模拟系统实现各种炫酷的特效
指纹Key全国产化电子元件推荐方案
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
Coinbase: basic knowledge, facts and statistics about cross chain bridge
Create VPC in AWS console (no plate)
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
补:注解(Annotation)
单片机串口数据处理(2)——uCOSIII+循环队列接收数据