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. 【Ubuntu14】Nginx+PHP5+Mysql记录

    这次因为工作原因,需要在Linux下进行开发.推荐的环境是Ubuntu14+Nginx+PHP+Mysql.环境搭建好之后,装上GIT,装上IDE,觉得Mysql命令界面麻烦又装了个Navicat.总 ...

  2. Scrum立会报告+燃尽图(3)选题

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2193 一.小组介绍 组长:刘莹莹 组员:朱珅莹 孙韦男 祝玮琦 王玉潘 ...

  3. 实现react中的自动保存--定时任务

    1. 定义和用法 setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式. setInterval() 方法会不停地调用函数,直到 clearInterval() 被调用或 ...

  4. PyalgoTrade 交易(五)

    我们继续采取简单的策略,这次模拟实际交易.这个想法很简单: 如果调整后的收盘价高于SMA(15),我们将进入多头仓位(我们下单买入市价). 如果调整后的收盘价低于SMA(15),我们退出多头头寸(我们 ...

  5. 51Nod 1006:最长公共子序列Lcs(打印LCS)

    1006 最长公共子序列Lcs  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 给出两个字符串A B,求A与B的最长公共子序列(子序列不要求是连续的). ...

  6. laravel 创建自定义全局函数

    全局函数的实现是依靠在初始化的时候,将helps.php或者functions.php直接进行了加载.而Laravel中bootstrap/autoload.php(laravel 5.5 貌似没有这 ...

  7. Linux function: unshare

    When a new process is created with the clone() system call, a set of flags is provided which tells t ...

  8. nodejs json-t 基本测试

    安装npm包 npm i json-templater or yarn add json-templater 基本代码 var render = require('json-templater/str ...

  9. nexus bower 集成使用

    创建nexus bower proxy host 比较简单,如下图: 安装bower && bower-nexus resolver npm install -g bower-nexu ...

  10. Dictionary字典类介绍

    说明     必须包含名空间System.Collection.Generic      Dictionary里面的每一个元素都是一个键值对(由二个元素组成:键和值)      键必须是唯一的,而值不 ...