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) {
char c[] = {'I','V','X','L','C','D','M'};
int n[] = {,,,,,,};
unordered_map<char,int> map;
map.insert(make_pair('I',));
map.insert(make_pair('V',));
map.insert(make_pair('X',));
map.insert(make_pair('L',));
map.insert(make_pair('C',));
map.insert(make_pair('D',));
map.insert(make_pair('M',)); int len = s.length();
int num = ;
if(len==){
num = map[s[]];
return num;
} for(int i = ; i < len; i++){
if(map[s[i]] <= map[s[i-]]){
num += map[s[i-]];
}
else{
num+= map[s[i]]-map[s[i-]];
i++; //dealt two letters at one time, so i should increase 2
}
} //Do not forget to discuss the last case independently
if(len > && map[s[len-]] <= map[s[len-]]){
num+=map[s[len-]];
} return num;
}
};

Improve: 使用两个数组代替map,更节省空间。

class Solution {
public:
int romanToInt(string s) {
int values[] = {, , , , , , };
char numerals[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I' };
int i = , j = , result = ;
while(i < s.length()){
if(s[i] != numerals[j]){
j++;
continue;
} if(i+<s.length() && j->= && s[i+] == numerals[j-]){
result += values[j-]-values[j];
i+=;
}
else if(i+<s.length() && j->= && s[i+] == numerals[j-]){
result += values[j-]-values[j];
i+=;
}
else{
result += values[j];
i++;
}
}
return result;
}
};

13.Roman to Integer (HashTable)的更多相关文章

  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

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  4. C# 写 LeetCode easy #13 Roman to Integer

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

  5. 13. Roman to Integer【leetcode】

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

  6. 【LeetCode】13. Roman to Integer (2 solutions)

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

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

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

  9. 13. Roman to Integer[E]罗马数字转整数

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

随机推荐

  1. Microsoft Office 2010 Service Pack 2 发布更新

    Microsoft Office 2010 32 位版本 的 Service Pack 2 (SP2) 包含的新更新可以提高安全性.性能和稳定性.此外,SP 是以前发布的所有更新的汇总. 下载更新补丁 ...

  2. Python selenium chrome 环境配置

    Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...

  3. 每天一个linux命令(网络):【转载】ping命令

    Linux系统的ping命令是常用的网络命令,它通常用来测试与目标主机的连通性,我们经常会说“ping一下某机器,看是不是开着”.不能打开网页时会说“你先ping网关地址192.168.1.1试试”. ...

  4. Ambiguous reference to member 'dataTask(with:completionHandle:)'错误

    在研究IOS的网络请求过程中,因为NSURLConnection已经过时,需要引用到URLSession var url:NSURL=NSURL(string: "http://3g.163 ...

  5. 【传输协议】https SSL主流数字证书都有哪些格式?

    一般来说,主流的Web服务软件,通常都基于两种基础密码库:OpenSSL和Java. Tomcat.Weblogic.JBoss等,使用Java提供的密码库.通过Java的Keytool工具,生成Ja ...

  6. Linux和Docker常用命令

    一.常用Linux 命令 ls -a(同时列出隐含文件), -l(输出一个比较完整的格式,除每个文件名外,增加显示文件 类型.权限.硬链接数.所有者名.组名.大小(byte).及时间信息-----简化 ...

  7. cocos2dx 3.2 事件机制

    一个sprite的情况 // oneSprite void HelloWorld::touchableSpriteTestOne() { Vec2 origin = Director::getInst ...

  8. myeclipse修改内存大小不足

    工具中修改设置Default VM Arguments   1 打开MyEclipse,如下图所示 2 打开Windows-> Preferences 3 然后选择右侧菜单的Java->I ...

  9. Linux学习系列之Nginx调优实战

    Nginx配置文件性能微调 全局的配置 user www-data; pid /var/run/nginx.pid; worker_processes auto; worker_rlimit_nofi ...

  10. EF-CodeFirst系列100

    .学习资料地址: CodeFirst:https://msdn.microsoft.com/zh-cn/data/jj193542 一.CodeFirst迁移(Migration) .工具--> ...