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. 自己的reset.css

    复制.粘贴 /* http://www.cnblogs.com/ele-cat Reset Stylesheet v1.0.1 2018-05-08 Author: Ele-cat - http:// ...

  2. php模板引擎之blade

    一.简介模板引擎 模板引擎是将网站的页面设计和PHP应用程序几乎完全分离的一种解决方案,它能让前端工程师专注页面搭建,让后台工程师专注功能实现,以便实现逻辑分离,让每个人发挥所长.模板引擎技术的核心是 ...

  3. zookeeper 节点启动时的更新机制

    使用zk的应用节点和zk数据本身的同步,当系统启动时使用zk配置的信息和zk本身存储不一致, 此时应存在一个更新机制将应用配置数据和zk数据更新一致. 启动时更新拉取zk配置中心的更新本地数据,以zk ...

  4. UOJ.386.[UNR #3]鸽子固定器(贪心 链表)

    题目链接 \(Description\) 选最多\(m\)个物品,使得它们的\((\sum vi)^{dv}-(s_{max}-s_{min})^{du}\)最大. \(Solution\) 先把物品 ...

  5. SmartSVN has inconsistent newlines解决方法

    SmartSVN has inconsistent newlines解决方法 点击 Project–>Setting,选择Working copy下的EOL-style,将Default EOL ...

  6. git 撤销本地修改

    git checkout file 例如:git checkout app/views/carts/_index_m.html.erb 可以先用 git status 查看差异 然后 git chec ...

  7. Java调用OCR进行图片识别

    使用Java语言,通过Tesseract-OCR对图片进行识别. 1.Tesseract-OCR 下载windows版本并安装. 2.程序如下: a.ImageIOHelper类 package OC ...

  8. Node.js数据库连接池操作通用工具(MySQL模块)

    出处:OSN开源站点数据库通用工具类,OSN源代码地址,https://github.com/obullxl/osnode-site,百度云演示站点,http://obullxl.duapp.com使 ...

  9. web压力测试工具(小而精)

    实际的测试过程中,我们一般都是采用A.B两台机器,一台跑Web服务,另外一台跑ab测试.也有的情况是单机对单机可能测不出结果,那就要采用很多台机器同是跑AB去请求一台机器进行测试,根据多台机器反馈的结 ...

  10. HDU 4815 Little Tiger vs. Deep Monkey(2013长春现场赛C题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4815 简单的DP题. #include <stdio.h> #include <st ...