当前位置:网站首页>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
边栏推荐
- IEEE Transactions on Industrial Informatics(TII)投稿须知
- RC低通滤波器的逆系统
- 【论文阅读】【3d目标检测】point transformer
- MYSQL查询至少连续n天登录的用户
- 【BIM+GIS】ArcGIS Pro2. 8 how to open Revit model, Bim and GIS integration?
- Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
- C语言: 指针的进阶
- SQL statement for adding columns in MySQL table
- AWS eks add cluster user or Iam role
- Coinbase:关于跨链桥的基础知识、事实和统计数据
猜你喜欢
QML advanced (V) - realize all kinds of cool special effects through particle simulation system
【论文阅读】【3d目标检测】point transformer
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
STM32上μC/Shell移植与应用
第四章 --- 了解标准设备文件、过滤器和管道
C语言:恶搞小游戏
229. Find mode II
C语言常用字符串处理函数
Understand the gut organ axis, good gut and good health
Iron and intestinal flora
随机推荐
那些年我面试过的Android开发岗总结(附面试题+答案解析)
Experience summary and sharing of the first prize of 2021 National Mathematical Modeling Competition
2020 is coming to an end, special and unforgettable.
Detailed explanation of life cycle component of jetpack
Record your own dataset with d435i, run orbslam2 and build a dense point cloud
Recursive call -- Enumeration of permutations
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Iron and intestinal flora
What is the thirty-six plan
Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.1.15.
AWS eks add cluster user or Iam role
[timing] empirical evaluation of general convolution and cyclic networks for sequence modeling based on TCN
【Echart】echart 入门
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
C语言:恶搞小游戏
补:注解(Annotation)
Apache Bench(ab 压力测试工具)的安装与使用
補:注解(Annotation)
【论文阅读】【3d目标检测】Improving 3D Object Detection with Channel-wise Transformer
顺序表的基本操作