当前位置:网站首页>[LeetCode]13. Roman numerals to integers thirty
[LeetCode]13. Roman numerals to integers thirty
2022-06-13 00:12:00 【PowerDon】
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 . Input to ensure 1 To 3999 Within the scope of .
Example 1:
Input : “III”
Output : 3
Example 2:
Input : “IV”
Output : 4
Example 3:
Input : “IX”
Output : 9
Example 4:
Input : “LVIII”
Output : 58
explain : L = 50, V= 5, III = 3.
Example 5:
Input : “MCMXCIV”
Output : 1994
explain : M = 1000, CM = 900, XC = 90, IV = 4.
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 .
public class Solution {
public int RomanToInt(string s) {
int[] RomanNums = new int[128];
RomanNums['I'] = 1;
RomanNums['V'] = 5;
RomanNums['X'] = 10;
RomanNums['L'] = 50;
RomanNums['C'] = 100;
RomanNums['D'] = 500;
RomanNums['M'] = 1000;
char[] Romans = s.ToCharArray();
int result = 0;
for(int i = 0; i < Romans.Length; i++){
if(i < Romans.Length-1){
if(RomanNums[Romans[i]] < RomanNums[Romans[i+1]]){
result -= RomanNums[Romans[i]];
}else{
result += RomanNums[Romans[i]];
}
}else{
result += RomanNums[Romans[i]];
}
}
return result;
}
}
边栏推荐
- MASA Auth - 从用户的角度看整体设计
- PMP registration conditions, time, cost, new version related information
- Tsinghua-Bosch Joint ML Center, THBI Lab:Chengyang Ying | 通过约束条件风险价值实现安全强化学习
- The e-commerce employee changed the product price to 10% off after leaving the company, and has been detained
- Online examination questions for September examination of financial management
- 63. different paths II
- What can PMP bring to you
- 63. 不同路径 II
- 2022美容师(技师)上岗证题目及答案
- 哲學和文學的區別
猜你喜欢
How to publish OSM maps locally and customize the mapping
【Matlab】二维曲线
What can PMP bring to you
Running of NCF dapr application instance
How to control the display and hiding of layergroup through transparency in leaflet
Test platform series (97) perfect the case part
【Matlab】符号计算
MASA Auth - 从用户的角度看整体设计
How to visit a website
H5时代leaflet中还在用DivIcon?
随机推荐
The e-commerce employee changed the product price to 10% off after leaving the company, and has been detained
测试平台系列(97) 完善执行case部分
How to quickly query the mobile phone number home and operator
【HCIE论述】STP-A
Day 3 of jsbom and DOM learning
Is divicon still used in leaflet in H5 era?
Start of u-boot_ Armboot analysis (I)
Accelerating with Dali modules
分公司能与员工签劳动合同么
如何利用华为云容灾解决方案替代灾备一体机
Initial experience of Huawei cloud Conference [Huawei cloud to jianzhiyuan]
USTC of China University of science and technology: Minrui Wang | distribution network voltage stabilization based on transformer Multi-Agent Reinforcement Learning
【Matlab】基础知识
2022年6月11日记:王老师的春天,混入
如何让矢量瓦片配图神器maputnik支持 geoserver
leaflet如何优雅的展示重叠点位的气泡窗口
[matlab] matrix transformation and matrix evaluation
Enterprise wechat H5_ Authentication, PC website, enterprise wechat scanning code, authorized login
Real time preview of PHP in browser by vscade
华为云弹性云服务器ECS使用【华为云至简致远】