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 ...
随机推荐
- Android学习小Demo(20)关于Fragment的应用
Android在3.0之后引入了Fragment的概念,我推測其想法可能仅仅是想更好地兼容大屏幕或者平板的开发,由于大屏幕能够展示很多其它的内容,而内容一多,逻辑有可能就乱,而利用Fragment,则 ...
- PHP邮件发送(转)
php带有内置的mail() 发送邮件函数,但是较为繁琐:建议上网下载一个PHPMailer:
- http报错之return error code:401 unauthorized
http报错之return error code:401 unauthorized 依据HTTP返回码所表示的意思应该是未授权,没有输入账号和password,因此解决方法就直接在HTTP包里面 ...
- Linux进程的状态转换图
http://blog.csdn.net/mu0206mu/article/details/7348618 ◆运行状态(TASK_RUNNING) 当进程正在被CPU执行,或已经准备就绪随时可由调度程 ...
- hdu 3911 Black And White(线段树)
题目连接:hdu 3911 Black And White 题目大意:给定一个序列,然后有M次操作: 0 l r:表示询问l,r中最大连续1的个数 1 l r:表示将l,r区间上的数取反 解题思路:线 ...
- Windows Cygwin Redis 安装(转)
在win平台下编译Redis一般有两种方式: 1. 基于MS VC进行编译,生成原生可执行文件 该方式需要创建MSVC项目文件以及对Redis源码进行适当调整. 这里提供一个可行版本,由微软开放团队进 ...
- 有愿意共同发展USB固件做?
有愿意共同发展USB固件做.现在,它使用STC89S52+PDIUSBD12(环教你玩USB开发板)它实现了一个USB键盘,项目地址:https://github.com/artprogramming ...
- 一个IT学生的personal statement
前一段时间的英语老师要求我们写一个自己的personal statement,我相信,作为一个IT学生,人很多personal statement应该都了如指掌.进一步的研究是必要的出国留学,当然,也 ...
- javaScript在私有的属性和方法
javaScript并没有什么特别的语法来代表私人.保.或公共的属性和方法,在这一点上与 java或其他语言是不同的.JavaScript大家是共同的所有对象: var myobj={ mypop:1 ...
- SQL Server 2008杀数据库连接
杀数据库连接: DECLARE @temp NVARCHAR(20) DECLARE myCurse CURSOR FOR SELECT spid FROM sys.sysprocesses WHER ...