当前位置:网站首页>Leetcode004 -- Roman numeral to integer
Leetcode004 -- Roman numeral to integer
2022-04-23 04:39:00 【singularityDZF】
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class test04 {
/**
* 13. Roman numeral to integer
* Roman numerals contain the following seven characters : I, V, X, L,C,D and M.
*
* character The number
* I 1
* V 5
* X 10
* L 50
* C 100
* D 500
* M 1000
* for example , Rome digital 2 Write to do II , Two parallel 1 .12 Write to do XII , That is to say X + II . 27 Write to do XXVII, That is to say XX + V + II .
* Usually , The small numbers in roman numbers are to the right of the big ones . But there are special cases , for example 4 Do not write IIII, It is IV. Numbers 1 In number 5 Left side , The number represented is equal to the large number 5 Decimal reduction 1 Value obtained 4 . similarly , Numbers 9 Expressed as IX. This special rule only applies to the following six cases :
*
* I Can be placed in V (5) and X (10) Left side , To express 4 and 9.
* X Can be placed in L (50) and C (100) Left side , To express 40 and 90.
* C Can be placed in D (500) and M (1000) Left side , To express 400 and 900.
* Given a Roman number , Convert it to an integer .
*
*
* Example 1:
* Input : s = "III"
* Output : 3
*
* Example 2:
* Input : s = "IV"
* Output : 4
*
* Example 3:
* Input : s = "IX"
* Output : 9
*
* Example 4:
* Input : s = "LVIII"
* Output : 58
* explain : L = 50, V= 5, III = 3.
*
* Example 5:
* Input : s = "MCMXCIV"
* Output : 1994
* explain : M = 1000, CM = 900, XC = 90, IV = 4.
*
* Tips :
* 1 <= s.length <= 15
* s Characters only ('I', 'V', 'X', 'L', 'C', 'D', 'M')
* Topic data assurance s It's a valid Roman number , And it means that the integer is in the range [1, 3999] Inside
* The test cases given in the title all conform to the Roman numeral writing rules , There will be no straddle, etc .
* IL and IM Such an example does not meet the requirements of the title ,49 You should write XLIX,999 You should write CMXCIX .
* Detailed rules for writing Roman numerals , You can refer to Rome digital - Mathematics .
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter Roman numerals :");
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://yzsam.com/2022/04/202204230438035459.html
边栏推荐
- C语言: 指针的进阶
- A lifetime of needs, team collaboration can play this way on cloud nailing applet
- VHDL implementation of 32-bit binary to BCD code
- IEEE Transactions on Industrial Informatics(TII)投稿须知
- Recursive call -- Enumeration of permutations
- 补充番外14:cmake实践项目笔记(未完待续4/22)
- Error occurs when thymeleaf th: value is null
- Huawei machine test -- high precision integer addition
- Go反射法则
- Supplément: annotation
猜你喜欢
[paper reading] [3D target detection] point transformer
Inverse system of RC low pass filter
协程与多进程的完美结合
Chapter 4 - understanding standard equipment documents, filters and pipelines
做数据可视化应该避免的8个误区
IDE Idea 自动编译 与 On Upate Action 、 On Frame Deactivation 的配置
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
Introduction to Cortex-M3 register set, assembly language and C language interface
洛谷P1858 【多人背包】 (背包求前k优解)
Opencv -- yoact case segmentation model reasoning
随机推荐
MYSQL去重方法汇总
Stm32f4 MCU ADC sampling and FFT of ARM-DSP Library
[pytoch foundation] torch Split() usage
IEEE Transactions on systems, man, and Cybernetics: Notes for systems (TSMC)
从MySQL数据库迁移到AWS DynamoDB
【Echart】echart 入門
Mysql---数据读写分离、多实例
Gut liver axis: host microbiota interaction affects hepatocarcinogenesis
那些年我面试过的Android开发岗总结(附面试题+答案解析)
2020 is coming to an end, special and unforgettable.
Why recommend you to study embedded
Redis command Encyclopedia
[AI vision · quick review of robot papers today, issue 32] wed, 20 APR 2022
What is the thirty-six plan
无线充电全国产化电子元件推荐方案
Create VPC in AWS console (no plate)
针对NFT的网络钓鱼
shell wc (统计字符数量)的基本使用
Installation and use of Apache bench (AB pressure test tool)
Go reflection rule