class Solution {
public:
int romanToInt(string s) {
if (s.length() < 1)
return 0;
map<char,int> m;
m['I'] = 1;
m['V'] = 5;
m['X'] = 10;
m['L'] = 50;
m['C'] = 100;
m['D'] = 500;
m['M'] = 1000;
int i = s.length() - 1;
int sum = m[s[i--]];
while (i >= 0) {
if (m[s[i + 1]] > m[s[i]])
sum -= m[s[i]];
else
sum += m[s[i]];
--i;
}
return sum;
}
};
class Solution {
public:
string intToRoman(int num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
string symbol[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int value[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string res = "";
int i = 0;
while (num > 0) {
if (num >= value[i]) {
res += symbol[i];
num -= value[i];
}
else
++i;
}
return res;
}
};

leetcode Roman Integer的更多相关文章

  1. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  2. [LeetCode][Python]Integer to Roman

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...

  3. [LeetCode] 12. Integer to Roman 整数转化成罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  4. [LeetCode] 12. Integer to Roman 整数转为罗马数字

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  5. [Leetcode] Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  6. 【leetcode】Integer to Roman

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  7. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  8. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  9. 【JAVA、C++】LeetCode 012 Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

随机推荐

  1. rsync基础

    参考资料:骏马金龙的rsync系列.该博主的博文质量很好,推荐大家关注. 环境 操作系统:CentOS Linux release 7.5.1804 (Core) 软件:rsync  version ...

  2. MySql 时间戳存char还是存int?

    一次小事故,让我对时间戳存char还是存int有了深刻的印象. 生产环境的sql条件涉及到时间戳字段的大小比较(between and),当时设计的时间戳类型是char(10),结果当数据量达到200 ...

  3. java集合系列之LinkList

    概要  第1部分 LinkedList介绍第2部分 LinkedList数据结构第3部分 LinkedList源码解析(基于JDK1.6.0_45) 第5部分 LinkedList示例 转载请注明出处 ...

  4. 新手学cocos2dx,centos7下的安装过程

    背景 打算学写游戏,新手向,当然从cocos2d-x开始. 看了cocos的文档,安装是针对ubuntu的,这里记录下centos7上安装.编译.运行测试的过程. 如果你已经有ubuntu,不推荐看此 ...

  5. JavaScript基础之运算符及全面的运算符优先级总结

    算数运算符: 加+,减—,乘*,除/,求余%,加加++,减减——, 加减乘除求余运算与数学上的用法完全一样. 不过,加号+还有连接字符串的作用,其他运算符还可以将字符串数字转换成数值型,参见JavaS ...

  6. C#远程调用技术WebService葵花宝典

    一.课程介绍 直接开门见山吧,在学习之前阿笨想问大家一句,关于WebService远程过程调用技术(RPC) 你真的会了吗?不要跟老夫扯什么WebService技术已经过时,如果你的内心有在偷偷告诉你 ...

  7. bat如何批量删除指定部分文件夹名的文件夹

    @echo offfor /f "delims=" %%i in ('dir /s/b/ad 123*') do ( rd /s/q "%%~i")exit

  8. 微软收购跨平台移动开发公司Xamarin

    摘要:北京时间2月25日早间消息,微软周三宣布收购创业公司Xamarin,这也是该公司为了吸引更多软件工程师为其云计算服务编写程序而采取的最新举措.古斯里表示,总部位于旧金山的Xamarin创立于20 ...

  9. ASP.NET Web API实践系列01,以ASP.NET Web Form方式寄宿

    创建一个空的ASP.NET Web Form项目. 右键项目,添加新项,创建Web API控制器类,TestController. 删除掉TestController默认的内容,编写如下: using ...

  10. iOS用全局宏的概念理解xcode中的设置 preprocessor macros

    ios有没有全局宏,或者在工程属性里设置宏? 比如我设置了一个宏叫IOS, 在所有/整个工程的代码里这个宏都是有效的. ------解决方案-------------------- 在工程的设置属性里 ...