【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列
(一)题目
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
(二)解题
和上一题相反,这题将罗马数字转换成整形数。
注意到 4,9,40,90,400,900这些特殊的数字的区别就不难写出代码了。
class Solution {
public:
int romanToInt(string s) {
int ret=0;
for(auto iter = s.begin() ; iter!=s.end() ; ++iter)
{
if((iter+1)!=s.end() && tonum(*iter) < tonum(*(iter+1)))
{
ret += tonum(*(iter+1))-tonum(*iter);
++iter;
}
else
ret += tonum(*iter);
}
return ret;
}
int tonum(char ch)
{
switch(ch)
{
case 'I' : return 1;
case 'V' : return 5;
case 'X' : return 10;
case 'L' : return 50;
case 'C' : return 100;
case 'D' : return 500;
case 'M' : return 1000;
}
}
};
【一天一道LeetCode】#13. Roman to Integer的更多相关文章
- Leetcode#13. Roman to Integer(罗马数字转整数)
题目描述 罗马数字包含以下七种字符:I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [leetcode]13. Roman to Integer罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- Git 处理tag和branch的命令
最近想给GitHub 上的项目设置tag,可是使用GitHub Desktop,找了一圈都没找到快速设置Tag 的地方,最后只能通过终端命令来添加了. 想要查看Git 的命令,可以使用 git --h ...
- ROS(indigo) 安装和使用更新版本的Gazebo----3,4,5,6,7 附:中国机器人大赛中型组仿真比赛说明
ROS(indigo) 安装和使用更新版本的Gazebo,本文以7为例. Gazebo7支持更多新的功能,如果使用下面命令安装ROS(indigo): ~$ sudo apt-get install ...
- 23 服务音乐的启动Demo4
注意如果音乐服务和Activity在一个应用中那么将不会因为绑定的Activity销毁而关闭音乐 MainActivity.java package com.qf.day23_service_demo ...
- 使用C++将OpenCV中Mat的数据写入二进制文件,用Matlab读出
在使用OpenCV开发程序时,如果想查看矩阵数据,比较费劲,而matlab查看数据很方便,有一种方法,是matlab和c++混合编程,可以用matlab访问c++的内存,可惜我不会这种方式,所以我就把 ...
- Github上的Android项目介绍之ListViewAnimation(针对listView item的侧滑菜单)(1)
demo源码,需要可以下载 1.这是一个github开源项目,先去github上面下载,github下载地址. 2.将SwipeMenuListView项目,导入,然后新建项目如果要引用,要设置为相应 ...
- android解析网络json数据(1)
1.首先获得url,传入URL类,利用URL的openconnection方法,获得URLConnection,去的输入流,进行操作,具体代码如下: public class NetConnectio ...
- Java基本语法-----java运算符
这块的东西比较多 我写了太慢了 于是在word里写好贴出来供大家一起学习 运算符 -赋值运算符 -比较运算符 -逻辑运算符 -位运算符 -移位操作符 -三元运算符 [正在看本人博客的这位童鞋,我看你气 ...
- Android水印相机
本篇文章实现的水印相机,类似于qq空间中的水印相机功能,因之前看过一个demo上实现了一个简陋的水印相机功能,觉得挺有意思,就在此基础上进行了修改,优化和完善,并增加了部分功能,使之更接近于qq水印相 ...
- (一二三)基于GCD的dispatch_once实现单例设计
要实现单例,关键是要保证类的alloc和init只被调用一次,并且被自身强引用防止释放. 近日读唐巧先生的<iOS开发进阶>,受益匪浅,通过GCD实现单例就是收获之一,下面把这个方法与大家 ...
- Dynamics CRM 后台通过组织服务获取时间字段值的准确转换
做CRM开发的都知道,在系统时间字段的处理上是有讲究的,因为数据库中存的是UTC时间,CRM的界面时间字段会根据个人设置中的时区以及格式自动调整,这是最基本的一面,那还有很多使用时间的场景,比如脚本使 ...