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. Eclipse 中 ctrl+鼠标左键 快捷查看资源失效

    Eclipse 中 ctrl+鼠标左键 快捷查看资源失效 看看eclipse 工作空间配置的jdk是什么版本,看看本地环境变量中的jdk是什么版本,将二者的版本统一即可. 笔者从git上clone一个 ...

  2. CS231n课程笔记翻译5:反向传播笔记

    译者注:本文智能单元首发,译自斯坦福CS231n课程笔记Backprop Note,课程教师Andrej Karpathy授权翻译.本篇教程由杜客翻译完成,堃堃和巩子嘉进行校对修改.译文含公式和代码, ...

  3. 接口测试工具Soapui5.1.2参数化之Properties20150924

    上次用天气预报的来给大家演示了下如何创建项目.测试套件.测试用例的操作,今天演示下如何参数化,废话不多说,跟着操作即可: 1.在一个用例中有两个步骤,我们想将第一个步骤中的响应中的值,传入第二个步骤中 ...

  4. 回测框架pybacktest简介(二)

    pybacktest 的疑点 第(一)节“教程”原文,是用 ipython notebook 写成,程序代码是一些片段组成. 为了阅读方便,合并在一起. 本文转载于:http://blog.csdn. ...

  5. 【LGR-051】洛谷9月月赛

    [LGR-051]洛谷9月月赛 luogu 签到题 description 给出\(K\)和质数\(m\),求最小的\(N\)使得\(111....1\)(\(N\)个\(1\))\(\equiv k ...

  6. sysfs中属性文件的建立

    1.device中建立属性文件 (1)函数调用关系: /**************************************************************/ device_c ...

  7. pandas groupby 使用

    so useful~ refer to: http://kekefund.com/2016/06/17/pandas-groupby/

  8. 关于Eclipse中import javax.servlet.*出错

    今天为了调试一下我写的Servlet,突然间,发现我的站点下所有的Servlet全部都出错了,仔细一看,原来是import javax.servlet.*这里出错了. 然后我就上网查阅了一些资料,才发 ...

  9. hadoop之 Hadoop 2.x HA 、Federation

    HDFS2.0之HA 主备NameNode: 1.主NameNode对外提供服务,备NameNode同步主NameNode元数据,以待切换: 2.主NameNode的信息发生变化后,会将信息写到共享数 ...

  10. Centos 6 安装 配置 oracle11g R2

    1.安装centos6.3_64位: 下载地址:http://mirror.bit.edu.cn/centos/6.3/isos/x86_64/ CentOS-6.3-x86_64-bin-DVD1. ...