[LeetCode OJ] Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
下面是百度得到的关于罗马数的解释:

我的代码:
class Solution {
public:
int romanToInt(string s) {
map<char,int> conversion;
conversion.insert(make_pair('I',));
conversion.insert(make_pair('V',));
conversion.insert(make_pair('X',));
conversion.insert(make_pair('L',));
conversion.insert(make_pair('C',));
conversion.insert(make_pair('D',));
conversion.insert(make_pair('M',));
int ans=;
for(unsigned i=; i<s.size(); i++)
{
if(s[i]=='V' || s[i]=='L' || s[i]=='D') //不能把基本数字V 、L 、D 中的任何一个作为小数放在大数的左边采用相减的方法构成数目
ans += conversion[s[i]];
else //基本数字Ⅰ、X 、C 中的任何一个,自身连用构成数目,或者放在大数的右边连用构成数目,都不能超过三个;放在大数的左边只能用一个
{
if(i<s.size()- && conversion[s[i]]<conversion[s[i+]])
{
ans += conversion[s[i+]]-conversion[s[i]];
i++;
}
else
ans += conversion[s[i]];
}
}
return ans;
}
};
[LeetCode OJ] Roman to Integer的更多相关文章
- [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 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【leetcode】Roman to Integer
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 【JAVA、C++】LeetCode 013 Roman to Integer
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- curl伪造ip
//随机IP function Rand_IP(){ $ip2id= round(rand(, ) / ); //第一种方法,直接生成 $ip3id= round(rand(, ) / ); $ip4 ...
- eclipse中java项目的build path详解(转载)
BuildPath中只支持加入jar文件,具体方法如下:在eclips里在工程名上右键->build path->contigure bud path->java build pat ...
- Nginx介绍 分类: Nginx 服务器搭建 2015-07-13 10:50 19人阅读 评论(0) 收藏
海量请求,高性能服务器. 对比Apache, Apache:稳定,开源,跨平台,重量级,不支持高度并发的web服务器. 由此,出现了Lighttpd与Nignx:轻量级,高性能. 发音:engine ...
- mybatis14 动态sql
动态sql(重点) mybatis重点是对sql的灵活解析和处理. 1.1需求 将自定义查询条件查询用户列表和查询用户列表总记录数改为动态sql 1.2if和where <!-- 自定义查询条件 ...
- mysql 学习笔记5-- 数据库优化
ext4:(rw,noatime,nodiratime,nobarrier,data=ordered)xfs: (rw,noatime,nodiratim,nobarrier,logbufs=8,lo ...
- Chapter 3 - How to Move a sprite
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=250529 http://www.cocos2d-x.org/boards/6/ ...
- Java基础知识强化之网络编程笔记04:UDP之发送端的数据来自于键盘录入案例
1. 数据来自于键盘录入 键盘录入数据要自己控制录入结束. 2. 代码实现: (1)发送端: package com.himi.updDemo1; import java.io.IOException ...
- Android ListView实现仿iPhone实现左滑删除按钮
需要自定义ListView.这里就交FloatDelListView吧. 复写onTouchEvent方法.如下: @Override public boolean onTouchEvent(Moti ...
- RedHat7笔记
第一章 管理网络 查看网络信息 显示网卡状态# nmcli dev status列出所有连接# nmcli con show只列出可用连接# nmcli con show --active显示网卡配 ...
- RedHat7搭建MongoDB
yum安装MongoDB 添加MongoDB源# vi /etc/yum.repos.d/mongodb-org-3.0.repo [mongodb-org-3.0] name=MongoDB Rep ...