13. Roman to Integer(第十一周)
来源:互联网 发布:linux互斥锁 实现 编辑:程序博客网 时间:2024/06/14 22:24
分析:
运用map把罗马字符转换成整数数字即可。再者就是了解罗马数的数字表示:
罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。记数的方法:
相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9;
在一个数的上面画一条横线,表示这个数增值 1,000 倍。
代码:
class Solution
{
public:
int romanToInt(string s)
{
map<char,int> my_map;
my_map['I'] = 1;
my_map['X'] = 10;
my_map['C'] = 100;
my_map['M'] = 1000;
my_map['V'] = 5;
my_map['L'] = 50;
my_map['D'] = 500;
int sum = my_map[s[s.size()-1]];
for(int i = 0;i < s.size()-1;i++)
{
if(my_map[s[i]] >= my_map[s[i+1]])
{
sum += my_map[s[i]];
}
else
{
sum -= my_map[s[i]];
}
}
return sum;
}
};
0 0
- 13. Roman to Integer(第十一周)
- 13. Roman to Integer (第一周)
- 第一周:12. Integer to Roman
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13.Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- 13. Roman to Integer
- Android与JavaSocket的简单示例
- Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with ......
- GreenDao3.0简单使用
- 机器学习-广义线性模型GLM
- pyenv 安装的python无法利用系统安装的mysql-connector的解决办法
- 13. Roman to Integer(第十一周)
- 数据库设计——评论回复功能
- 状压dp(3)
- c_SeqList
- Android框架之路——Glide加载图片(结合RecyclerView、CardView)
- 齿轮式列表实现代码
- ConvertUtils
- php文件处理与文件上传
- Java入门学习好书