Leetcode 13 Roman to Integer 字符串处理+STL
题意:将罗马数字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的更多相关文章
- 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 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 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 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [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 ...
- [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 ...
随机推荐
- golang--gopher北京大会(2)(rework)
三.七牛老许 qlang: github qiniu/qlang microservice architecture: http://martinfowler.com/articles/microse ...
- HTML5和CSS3基础教程(第8版)-读书笔记
第1章 网页的构造块 一个网页主要包括以下三个部分: n 文本内容(text content):在页面上让访问者了解页面内容的纯文字. n 对其他文件的引用(referen ...
- 用过的一个jquery插件实现转盘效果还不错手机兼容
(function($) {var supportedCSS,styles=document.getElementsByTagName("head")[0].style,toChe ...
- iOS字符串为空的判断
//判断字符串 -(BOOL) isValidString:(id)input { if (!input) { return NO; } if ((NSNull *)input == [NSNull ...
- HLSL之漫反射光
整整忙了一个月了,总算清闲下来了,从上次写完环境光后又过了这么长时间,继续学习......加油!!今天整理下漫反射光并记录下来,那就直接进入主题吧,开始漫反射光的学习. 漫反射光是在环境光的基础上添加 ...
- cocos2d-x之内存管理(4)
c++的内存管理一直以来都是个问题,也有多种实现方案,比如智能指针,使用引用计数等,cocos2d-x也需要涉及到内存的管理. cocos2d-x是如何管理内存的呢? cocos2d-x的内存管理主要 ...
- UWP深入学习六:Build better apps: Windows 10 by 10 development series
Promotion in the Windows Store In this article, I walk through how to Give your Store listing a mak ...
- TCP/UDP socket
TCP socket:有链接,绑定端口,接着去侦听,若有请求,那么accept(),获得新的socket,并且去接收/发送数据报. UDP socket:无连接,不需要侦听,也不用一个新的socket ...
- nodejs:使用多处理器
nodejs是单线程,这意味着Node只能利用一个处理器来工作.但多数服务器都有多个核.好在nodejs提供了cluster模块,可以把任务分配给子进程.每个子进程有些特殊能力,比如能与其他子进程共享 ...
- Event --mysql的scheduler.md
事件调度器event 相当于oracle scheduler CREATE [DEFINER = { user | CURRENT_USER }] EVENT [IF NOT EXISTS] even ...