当前位置:网站首页>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
边栏推荐
- Error occurs when thymeleaf th: value is null
- Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
- Bacterial infection and antibiotic use
- The perfect combination of collaborative process and multi process
- C语言: 指针的进阶
- Installation and use of Apache bench (AB pressure test tool)
- Recursive call -- Enumeration of permutations
- Why recommend you to study embedded
- Leetcode->1 两数之和
- AWS EKS 部署要点以及控制台与eksctl创建的差异
猜你喜欢
无线键盘全国产化电子元件推荐方案
补充番外14:cmake实践项目笔记(未完待续4/22)
Bridge between ischemic stroke and intestinal flora: short chain fatty acids
Ali's ten-year technical experts jointly created the "latest" jetpack compose project combat drill (with demo)
【论文阅读】【3d目标检测】Voxel Transformer for 3D Object Detection
Supplement 14: cmake practice project notes (to be continued 4 / 22)
第四章 --- 了解标准设备文件、过滤器和管道
[echart] démarrer avec echart
Supplement: Annotation
Recursive call -- Enumeration of permutations
随机推荐
记录一下盲注脚本
Opencv -- yoact case segmentation model reasoning
[AI vision · quick review of today's sound acoustic papers, issue 2] Fri, 15 APR 2022
Cortex-M3寄存器组、汇编语言与C语言的接口介绍
单片机串口数据处理(2)——uCOSIII+循环队列接收数据
Go反射—Go语言圣经学习笔记
Go reflection rule
Key points of AWS eks deployment and differences between console and eksctl creation
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
兼容NSR20F30NXT5G的小体积肖特基二极管
What is the thirty-six plan
针对NFT的网络钓鱼
Summary of MySQL de duplication methods
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
阿里十年技术专家联合打造“最新”Jetpack Compose项目实战演练(附Demo)
AWS eks add cluster user or Iam role
520. Detect capital letters
Brushless motor drive scheme based on Infineon MCU GTM module
在AWS控制台创建VPC(无图版)
2019 is coming to an end, the longest day.