leetcode第13题--Roman to Integer
Problem:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数。其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000}
class Solution {
public:
int romanToInt(string s)
{
string refe = "IVXLCDM";
int val[] = {, , , , , , };
int result = ;
for (int i = ; i < s.size(); i++)
{
for (int j = ; j < refe.length(); j++)
{
if (i+<s.size() && s[i] == refe[j] && ((j+<refe.size() && s[i+] == refe[j+]) || (j+<refe.size() && s[i+] == refe[j+])))
{result -= val[j];}
else if (s[i] == refe[j])
{result += val[j];}
}
}
return result;
}
};
leetcode第13题--Roman to Integer的更多相关文章
- 【LeetCode算法-13】Roman to Integer
LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...
- LeetCodeOJ刷题之13【Roman to Integer】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- LeetCode第[13]题(Java):Roman to Integer
题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- 【算法】LeetCode算法题-Roman To Integer
这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...
- leetcode第八题--String to Integer (atoi)
Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...
- leetcode第八题 String to Integer (atoi) (java)
String to Integer (atoi) time=272ms accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...
- [leetcode]经典算法题- String to Integer (atoi)
题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...
- 【LeetCode每天一题】Reverse Integer(反转数字)
Given a 32-bit signed integer, reverse digits of an integer. Example 1: ...
- [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 ...
随机推荐
- Mapxtreme C#鹰眼地图
Demo演示程序下载地址: http://pan.baidu.com/s/1jG9gKMM#dir/path=%2F%E4%BA%A7%E5%93%81%2FDemos 找:EagelEyeMap.r ...
- Team Foundation Server 2015使用教程--默认团队checkin权限修改
- 朝花夕拾-4-shell
引言 shell,我们常常会用到,以其强大的功能,会帮助我们解决非常多棘手的问题.近期遇到一个问题,要跑非常多case,假设串行的执行,须要非常久.能不能让他们并行起来,但又不能全部case都并行执行 ...
- hibernate Disabling contextual LOB creation as connection was null
使用hibernate通路sybase当遇到异常. Could not obtain connection metadata : ASE is now using a multi-byte c ...
- Lucene于Directory
MMapDirectory从继承FSDirectory,抵抗jre至今未能解决Mmap close不回收空间(直到full gc恢复之前,)的bug,lucene使用hack资料恢复(只要sun ja ...
- [程序安装包制作] Advanced Installer 备忘
原文:[程序安装包制作] Advanced Installer 备忘 Product Information - Product Details 这个重点是Product Version.讲这个之前, ...
- oracle从备份归档日志的方法集中回收
oracle从备份集中抓出归档日志方法 在大连医院遇到这个问题,数据库为归档状态,但归档完成后rman通过crontab自己主动备走归档日志并删除存在系统上的归档日志文件.在RealSync程序停止一 ...
- Android AIDL使用特定的解释
1.什么是aidl:aidl这是 Android Interface definition language缩写,认清,这是android进程间通信接口的叙事语言描述.通过它我们可以定义进程间通信接口 ...
- SAE+Java+jetty
SAE的java执行环境jetty如servlet该容器,和开放几乎完全访问.有效支持各种jar包.但对于jdk的要件的版本号是非常严格格,这里的版本号是jdk1.6 SAE利用上传war该方式配置j ...
- 如何使用Ubuntu打电话
在这个视频,我们学习如何使用Ubuntu打电话.Ubuntu手机的很多用户谁是不是很熟悉. 特别是,他什么都无所谓的物理按键(菜单键.home纽带.回车键).然后用户如何控制手机它?Ubuntu手机凭 ...