当前位置:网站首页>13. Roman to Integer罗马数字转整数
13. Roman to Integer罗马数字转整数
2022-07-23 18:23:00 【DXB2021】
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, 2 is written as II in Roman numeral, just two ones added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.
例如, 罗马数字 2 写做 II ,即为两个并列的 1 。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I can be placed before V (5) and X (10) to make 4 and 9.
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X can be placed before L (50) and C (100) to make 40 and 90.
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C can be placed before D (500) and M (1000) to make 400 and 900.
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
Given a roman numeral, convert it to an integer.
给定一个罗马数字,将其转换成整数。
Example 1:
示例 1:
Input: s = "III"
Output: 3
Explanation: III = 3.
输入: s = "III"
输出: 3
Example 2:
示例 4:
Input: s = "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.
输入: s = "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.
Example 3:
示例 5:
Input: s = "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
输入: s = "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.
示例 2:
输入: s = "IV"
输出: 4
示例 3:
输入: s = "IX"
输出: 9
Constraints:提示:
1 <= s.length <= 15
s contains only the characters ('I', 'V', 'X', 'L', 'C', 'D', 'M').
s 仅含字符 ('I', 'V', 'X', 'L', 'C', 'D', 'M')
It is guaranteed that s is a valid roman numeral in the range [1, 3999].
题目数据保证 s 是一个有效的罗马数字,且表示整数在范围 [1, 3999] 内
题目所给测试用例皆符合罗马数字书写规则,不会出现跨位等情况。
IL 和 IM 这样的例子并不符合题目要求,49 应该写作 XLIX,999 应该写作 CMXCIX 。
关于罗马数字的详尽书写规则,可以参考 罗马数字 - Mathematics 。
C语言:失败
int romanToInt(char * s){
int num=0;
for(int i=0;s[i]!='\0';i++)
{
if(s[i]=='I')
{
if(s[i+1]=='V')
{
num+=4;
i++;
}
else if(s[i+1]='X')
{
num+=9;
i++;
}
else
num+=1;
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{
num+=40;
i++;
}
else if(s[i+1]=='C')
{
num+=90;
i++;
}
else
num+=10;
}
else if(s[i]=='C')
{
if(s[i+1]=='D')
{
num+=400;
i++;
}
else if(s[i+1]=='M')
{
num+=900;
i++;
}
else
num+=100;
}
else
{
if(s[i]=='V')
num+=5;
else if(s[i]=='L')
num+=50;
else if(s[i]=='D')
num+=500;
else if(s[i]=='M')
num+=1000;
}
}
return num;
}C语言:失败
int romanToInt(char * s){
int num=0;
for(int i=0;i<strlen(s);)
{
if(s[i]=='V')
{
num+=5;
i++;
}
else if(s[i]=='L')
{
num+=50;
i++;
}
else if(s[i]=='D')
{
num+=500;
i++;
}
else if(s[i]=='M')
{
num+=1000;
i++;
}
else if(s[i]=='I')
{
if(s[i+1]=='V')
{
num+=4;
i+=2;
}
else if(s[i+1]='X')
{
num+=9;
i+=2;
}
else
{
num+=1;
i++;
}
}
else if(s[i]=='X')
{
if(s[i+1]=='L')
{
num+=40;
i+=2;
}
else if(s[i+1]=='C')
{
num+=90;
i+=2;
}
else
{
num+=10;
i++;
}
}
else if(s[i]=='C')
{
if(s[i+1]=='D')
{
num+=400;
i+=2;
}
else if(s[i+1]=='M')
{
num+=900;
i+=2;
}
else
{
num+=100;
i++;
}
}
}
return num;
}C语言:
int romanToInt(char * s){
int num=0;
for(int i=0;i<strlen(s);i++)
{
switch(s[i])
{
case 'M':num+=1000;break;
case 'D':num+=500;break;
case 'L':num+=50;break;
case 'V':num+=5;break;
case 'C':switch(s[i+1])
{
case 'M':num-=100;break;
case 'D':num-=100;break;
default:num+=100;break;
}break;
case 'X':switch(s[i+1])
{
case 'L':num-=10;break;
case 'C':num=-10;break;
default:num+=10;break;
}break;
case 'I':switch(s[i+1])
{
case 'V':num-=1;break;
case 'X':num-=1;break;
default:num+=1;break;
}break;
}
}
return num;
}边栏推荐
- Huawei cloud stack [interview]
- USB3.0:VL817Q7-C0的LAYOUT指南
- 20. Valid Parentheses有效的括号
- (dry goods) introduce several common feature selection methods combined with scikit learn
- 能量原理与变分法笔记17:广义变分原理(识别因子方法)
- Powercli add esxi host to vCenter
- Sui of the public chain (New Public chain project established by former Facebook /meta employees)
- Redux求和案例详解版教程
- Industry analysis | logistics intercom
- 当代励志“女”录
猜你喜欢

According to the e-commerce written on the resume, how does redis realize inventory deduction and prevent oversold?

Publish the local image to Alibaba cloud warehouse

MySQL 啥时候用表锁,啥时候用行锁?

进程调度的基本过程

MySQL数据库【数据库基础--引入篇】

MySQL data recovery - using the data directory

看完这篇,彻底搞懂 gRPC!

2、 MFC windows and messages

PowerCLi 管理VMware vCenter 一键批量部署OVA

Canvas draw text and clear draw
随机推荐
C language leak detection and filling (1)
入门数据库days2
R language filters the data columns specified in dataframe, and R language excludes (deletes) the specified data columns (variables) in dataframe
Industry analysis | logistics intercom
四旋翼飞行器1——结构和控制原理
PowerCLi 管理VMware vCenter 批量部署导出导入
R语言使用quantile函数计算向量数据或者dataframe指定数据列的分位数(百分位数)
Summarize some recent tricks
MySQL 数据恢复 —— 使用 data 目录
Monotonic queue optimization DP
Paddle implementation, multidimensional time series data enhancement, mixup (using beta distribution to make continuous random numbers)
paddle实现,多维时序数据增强 ,mixup(利用beta分布制作连续随机数)
socat 使用「建议收藏」
C语言的查漏补缺(1)
能量原理与变分法笔记18:虚力原理
R language uses the gather function of tidyr package to convert a wide table to a long table (wide table to long table), the first parameter specifies the name of the new data column generated from th
测试如何应对新的开发模式?
White paper on adaptive robot interaction
能量原理与变分法笔记19:最小余能原理+可能功原理
能量原理与变分法笔记17:广义变分原理(识别因子方法)