当前位置:网站首页>Roman numeral to integer
Roman numeral to integer
2022-07-05 22:43:00 【dying_ star】
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 .
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/roman-to-integer
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
From selected reviews
The problem is very simple when you understand it . So let's set up a HashMap To map symbols and values , Then the string from left to right , If the value represented by the current character is not less than its right , Add this value ; Otherwise, subtract the value . And so on to the leftmost number , The final result is the answer
class Solution {
public int romanToInt(String s) {
HashMap<Character,Integer> hm=new HashMap<>();
int[] v={1,5,10,50,100,500,1000};
char[] k={'I','V','X','L','C','D','M'};
for(int j=0;j<7;j++){
hm.put(k[j],v[j]);
}
// Create a hashmap Realize the mapping between Roman numerals and integers
int flag=hm.get(s.charAt(0));//flag Point to the current number
int sum=0;// Define an accumulator
for(int i=1;i<s.length();i++){
char key=s.charAt(i);// Traverse the Roman string
int flagnext= hm.get(key);//flagnext Point to flag The next number , Through cyclic update
if(flag<flagnext){
sum-=flag;
}
else{
sum+=flag;}
flag=flagnext;// to update flag
}
sum+=flag;//flag At this point, it points to the last number , Not accumulated in the loop , Add here
return sum;
}
}
边栏推荐
- Go language learning tutorial (XV)
- The difference between MVVM and MVC
- Draw a red lantern with MATLAB
- Solve the problem of "no input file specified" when ThinkPHP starts
- Nangou Gili hard Kai font TTF Download with installation tutorial
- 【无标题】
- [Chongqing Guangdong education] National Open University autumn 2018 0088-21t Insurance Introduction reference questions
- C language - structural basis
- Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
- Binary tree (III) -- heap sort optimization, top k problem
猜你喜欢
Starting from 1.5, build a micro Service Framework -- log tracking traceid
链表之双指针(快慢指针,先后指针,首尾指针)
d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
What about data leakage? " Watson k'7 moves to eliminate security threats
700. Search in a Binary Search Tree. Sol
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
My experience and summary of the new Zhongtai model
关于MySQL的30条优化技巧,超实用
Distance entre les points et les lignes
Editor extensions in unity
随机推荐
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Distance entre les points et les lignes
南京:全面启用商品房买卖电子合同
Golang writes the opening chapter of selenium framework
第一讲:蛇形矩阵
Unity Max and min constraint adjustment
Sub total of Pico development
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
30 optimization skills about mysql, super practical
Damn, window in ie open()
點到直線的距離直線的交點及夾角
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
Draw a red lantern with MATLAB
Binary tree (II) -- code implementation of heap
70. Climbing Stairs. Sol
BFC block level formatting context
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
Kubernetes Administrator certification (CKA) exam notes (IV)
Postman核心功能解析-参数化和测试报告
[groovy] groovy dynamic language features (automatic type inference of function arguments in groovy | precautions for function dynamic parameters)