【一天一道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 ...
随机推荐
- Android Studio精彩案例(四)《DrawerLayout使用详解仿网易新闻客户端侧边栏 》
转载本专栏文章,请注明出处,尊重原创 .文章博客地址:道龙的博客 为了提高兴趣,咱们开头先看看最终要实现什么样的效果: 侧拉菜单在Android应用中非常常见,它的实现方式太多了,今天我们就说说使用G ...
- Android通过WebService实现图片的上传和下载(一)
这篇文章将讲解Android如果通过访问WebService接口实现图片的上传和下载,当然这不但需要大家懂得Android还要懂得WebService技术,安卓属于客户端,而webservice则属于 ...
- 无需密码通过scp命令+key的方式实现文件传输
如果觉得scp每次都要输入密码很麻烦, 那么这是解决方案.假设你平时在windows上开发,用户名是xiang, 你有一台Ubuntu服务器wdksw.com, 用户名是root.现在你准备上传一些文 ...
- 手把手图文并茂教你发布Android开源库
转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼,文章链接: http://blog.csdn.net/hejjunlin/article/details/52452220 经常逛githu ...
- Android简易实战教程--第二十二话《自定义组合控件模拟qq登录下拉框和其中的一些”小技巧”》
转载此文章请注明出处:点击打开链接 http://blog.csdn.net/qq_32059827/article/details/52313516 首先,很荣幸此专栏能被CSDN推荐到主页.荣 ...
- cassandra 如何写数据以及放置副本
application发送数据到server application 发送请求到server 根据设置的load balance 规则从cluster中挑选一个coordinator,一般使用轮询即可 ...
- Web开发学习之路--Springmvc+Hibernate之初体验
本来想继续学习android的,可是用到了android和服务器交互,需要实现个login的功能,苦于没有这么个环境,那就只能自己来搭建了.既然已经基本上可以玩web了,那么接下来使用web开源的框架 ...
- Android 之Toast讲解-android学习之旅(一)
Toast比较常用,用于显示简短的提醒,比如网络连接断开等. Toast的简单编码实例 findViewById(R.id.button1).setOnClickListener(new OnClic ...
- 操作系统处理内存时内存页为4k
windows和unix处理内存时,一个内存页的大小都为4k. 测试代码 int main() { while (1) { int *p = (int *)malloc(1024); getchar( ...
- (七十九)MapKit的基本使用
MapKit是苹果公司开发的用于显示地图和实现定位.导航的地图框架. MapKit View可以通过storyboard.xib创建,也可以通过代码直接创建. 需要注意的是,通过storyboard和 ...