题意:将罗马数字1到3999转化成自然数字,这里用了STL库map将罗马字符映射到自然数字。

I,V,X,L,C,D,M -> 1,5,10,50,100,500,1000

m[s[i]]<m[s[i+1]//如IV 就需要减去1
 class Solution {
public:
map<char,int> m;
Solution(){
const int N = ;
char str[N+] = "IVXLCDM";
int num[N] ={,,,,,,};
for (int i = ; i < N; ++i){
m[str[i]] = num[i];
}
}
~Solution(){
m.clear();
}
int romanToInt(string s) {
int ans = ;
for(int i = ;i<s.size()-;++i){
if (m[s[i]]<m[s[i+]]) ans -= m[s[i]];
else ans += m[s[i]];
}
ans += m[s[s.size() - ]];
return ans;
}
};

Leetcode 13 Roman to Integer 字符串处理+STL的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. [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 ...

  4. 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 ...

  5. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

  6. LeetCode 13. Roman to Integer(c语言版)

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

  7. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. HTML5和CSS3基础教程(第8版)-读书笔记(2)

    第7章 CSS构造模块 7.1 构造样式规则 样式表中包含了定义网页外观的规则.样式表中的每条规则都有两个主要部分:选 择 器(selector) 和 声 明 块(declaration block) ...

  2. adb通信原理分析

    关于这个问题,自己研究了一下,没有研究出来 在网络上搜罗了一下,基本上关于ADB的原理用了两张图表示:        我表示没有看懂这两个图, 又开始查阅一些一些资料: 首先知道adb的通信有Sock ...

  3. hdu 5748(LIS) Bellovin

    hdu 5748 Peter有一个序列a1,a2,...,ana_1,a_2,...,a_na​1​​,a​2​​,...,a​n​​. 定义F(a1,a2,...,an)=(f1,f2,...,fn ...

  4. 面试知识点总结之Java语言

    1.如果某个对象出现在字符串表达式中,如System.out.println(this+".class");,则会自动调用this.toString() 2.所有的类都是在对其第一 ...

  5. linux上安装jdk并添加环境变量

    ==========ubuntu============================= http://www.oracle.com/technetwork/java/javasebusiness/ ...

  6. unreal3之FName及潜在bug

    FName是unreal3里对字符串高效处理的一种机制 基本原理就是把字符串hash存起来,然后每个字符串就只需要用一个数组索引值来表示了 FName的属性: NAME_INDEX Index; IN ...

  7. 从DNS配置

    从服务器可以从主服务器上抓取指定的区域数据文件起到备份解析记录和负载均衡的作用. 主DNS服务器IP:192.168.16.20 从DNS服务器IP:192.168.16.30 1,修改主服务器区域配 ...

  8. anelife

    无论如何要把安e生活做到最棒! 1.先用脑再用力!

  9. Ubuntu12.04配置mod_python

    安装: sudo apt-get install libapache2-mod-python python-mysqldb 然后编辑配置文件/etc/apache2/sites-enabled/000 ...

  10. jQuery学习总结(二)

    简单选择器: 在使用jQuery 选择器时,我们首先必须使用“$()”函数来包装我们的CSS 规则. 而CSS 规则作为参数传递到jQuery 对象内部后,再返回包含页面中对应元素的jQuery 对象 ...