LeetCode题解——Roman to Integer
题目:
将罗马数字转换为整数。
解法:
可以参考上一篇数字转换为罗马数字的规则。
代码:
class Solution {
public:
int sym2int(char sym) //罗马数字字符与数字的对应关系
{
switch(sym)
{
case 'I' : return ;
case 'V' : return ;
case 'X' : return ;
case 'L' : return ;
case 'C' : return ;
case 'D' : return ;
case 'M' : return ;
default : return ;
}
} int romanToInt(string s) {
int result = sym2int(s[]);
for(int i = ; i < s.size(); ++i)
{
result += sym2int(s[i]);
if(sym2int(s[i]) > sym2int(s[i-])) //如果当前字符比上一字符对应的值要大,说明这是一个减的过程,由于上一字符之前计算过,所以减2倍
result -= * sym2int(s[i-]);
}
return result;
}
};
LeetCode题解——Roman to Integer的更多相关文章
- [LeetCode 题解]: Roman to Interger
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a ro ...
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 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:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- 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, D and M. Symbol Value I 1 ...
随机推荐
- Vimrc配置以及Vim的常用操作
""""""""""""""""&quo ...
- 500G JAVA视频网盘分享 (Jeecg社区)
http://blog.csdn.net/zhangdaiscott/article/details/18220411 csdn 排名400多名 500 G JAVA视频网盘分享(Jeecg社区 ...
- Sina App Engine(SAE)入门教程(1)
此教程只针对刚接触SAE的小白用户,资深码农.高手请绕道.首先还是一个经典的实例,hello sae. 创建应用 在注册完账号之后,需要到 http://sae.sina.com.cn/?m=myap ...
- Delphi 发展历史
自然人的软件著作权,保护期为自然人终生及其died后50年:软件是合作开发的,截止于最后died的自然人died后第50年的12月31日.法人或者其他组织的软件著作权,保护期为软件首次发表之后50年, ...
- linux下,如何把整个文件夹上传到服务器(另一台linux)
1.Linux下目录复制:本机->远程服务器 scp -r /home/shaoxiaohu/test1 zhidao@192.168.0.1:/home/test2 #test1为源目录, ...
- [leetcode] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...
- openfire开发
openfire github地址:https://github.com/igniterealtime/Openfire 1.下载源代码:http://www.igniterealtime.org/d ...
- SimpleDateFormat日期格式化
public class T { /** * @param args */ public static void main(String[] args) { // TODO Auto-generate ...
- Javascript如何判断一个变量是数字类型?
isNaN()不能判断一个变量是否为数字类型,isNaN(123)值为false,isNaN('123')值也为false.isNaN() 的实际作用跟它的名字isNaN并不一致,isNaN(NaN) ...
- BZOJ 3123 森林(函数式线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3123 题意: 思路:总的来说,查询区间第K小利用函数式线段树的减法操作.对于两棵树的合并 ...