13.Roman to Integer (HashTable)
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)的更多相关文章
- 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 ,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 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 ...
- 13. Roman to Integer[E]罗马数字转整数
题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
随机推荐
- protel99 se中出现许多Backup of 文件,修改过保存时,总会出现备份文件,怎么才能取消这一设置?
在file选项左边有个向下的大箭头标示 点开 有个prefereces项 点开之 把create backup项勾掉即可
- vue music 歌单组件
在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...
- python-websocket-server hacking
/************************************************************************* * python-websocket-server ...
- Codeforces 165 E. Compatible Numbers【子集前缀和】
LINK 题目大意 给你一个数组,问你数组中的每个数是否可以在数组里面找到一个数和他and起来是0,如果可以就输出这个数,否则就输出-1 思路 首先很显然的是可以考虑找到每个数每一位都取反的数的子集 ...
- Objective-C教程备忘单
终极版本的Objective-C教程备忘单帮助你进行iOS开发. 想开始创建你的第一个iOS应用程序么?那么看一下这篇很棒的教程吧:Create your first iOS 7 Hello Worl ...
- mybatis异常:Could not find result map Java.util.Map 问题分析及解决 定位不到具体的位置的错误
mybatis异常:Could not find result map Java.util.Map 问题分析及解决 报这个错误呢,很难受的就是你定位不到具体的地方,找不到位置修改,你只知道有错误,但是 ...
- 【转】vim环境设置和自动对齐
原文网址:http://blog.chinaunix.net/uid-23525659-id-4340245.html 注:如果是用vim编写代码,建议开启vim的文件类型自动检测功能,这样编写代码换 ...
- java 线程池 ExeutorService
Java线程池 ExecutorService 原文:https://blog.csdn.net/suifeng3051/article/details/49443835/ 本篇主要涉及到的是java ...
- app添加引导页
1.设置guide.html 2.登陆或者主页面引用guide.html mui.plusReady(function() { //读取本地存储,检查是否为首次启动 决定是否显示引导页 var sho ...
- 【并发编程】Executor架构介绍
要点总结 Executor表示的任务类型 主要有3种: Runnable: 无返回值,无异常抛出: Callable:有返回值,可以异常抛出: Future任务: 表示异步计算,可取消: 通过newT ...