当前位置:网站首页>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
边栏推荐
- Coinbase: basic knowledge, facts and statistics about cross chain bridge
- Inverse system of RC low pass filter
- 2021数学建模国赛一等奖经验总结与分享
- leetcode001--返回和为target的数组元素的下标
- Go reflection - go language Bible learning notes
- A new method for evaluating the quality of metagenome assembly - magista
- Supplement 14: cmake practice project notes (to be continued 4 / 22)
- 2019 is coming to an end, the longest day.
- Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
- Unipolar NRZ code, bipolar NRZ code, 2ASK, 2FSK, 2PSK, 2DPSK and MATLAB simulation
猜你喜欢
The perfect combination of collaborative process and multi process
基于英飞凌MCU GTM模块的无刷电机驱动方案开源啦
229. Find mode II
補:注解(Annotation)
Summary of MySQL de duplication methods
Detailed explanation of life cycle component of jetpack
Supplément: annotation
AWS EKS添加集群用户或IAM角色
洛谷P1858 【多人背包】 (背包求前k优解)
Jetpack 之 LifeCycle 组件使用详解
随机推荐
洛谷P1858 【多人背包】 (背包求前k优解)
电钻、电锤、电镐的区别
C语言:恶搞小游戏
[AI vision · quick review of today's sound acoustic papers, issue 3] wed, 20 APR 2022
Nature medicine reveals individual risk factors of coronary artery disease
Go reflection rule
IEEE Transactions on Systems, Man, and Cybernetics: Systems(TSMC)投稿须知
MYSQL去重方法汇总
The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video
Iron and intestinal flora
Eksctl deploying AWS eks
[AI vision · quick review of NLP natural language processing papers today, No. 32] wed, 20 APR 2022
兼容NSR20F30NXT5G的小体积肖特基二极管
Recommended scheme for national production of electronic components for wireless charging
Apache Bench(ab 压力测试工具)的安装与使用
Go reflection - go language Bible learning notes
Youqilin 22.04 lts version officially released | ukui 3.1 opens a new experience
test
QML advanced (IV) - drawing custom controls
补充番外14:cmake实践项目笔记(未完待续4/22)