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的更多相关文章

  1. 【LeetCode算法-13】Roman to Integer

    LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...

  2. LeetCodeOJ刷题之13【Roman to Integer】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  3. LeetCode第[13]题(Java):Roman to Integer

    题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  4. 【算法】LeetCode算法题-Roman To Integer

    这是悦乐书的第145次更新,第147篇原创 今天这道题和罗马数字有关,罗马数字也是可以表示整数的,如"I"表示数字1,"IV"表示数字4,下面这道题目就和罗马数 ...

  5. leetcode第八题--String to Integer (atoi)

    Problem: Implement atoi to convert a string to an integer. Hint: Carefully consider all possible inp ...

  6. leetcode第八题 String to Integer (atoi) (java)

    String to Integer (atoi) time=272ms   accepted 需考虑各种可能出现的情况 public class Solution { public int atoi( ...

  7. [leetcode]经典算法题- String to Integer (atoi)

    题目描述: 把字符串转化为整数值 原文描述: Implement atoi to convert a string to an integer. Hint: Carefully consider al ...

  8. 【LeetCode每天一题】Reverse Integer(反转数字)

    Given a 32-bit signed integer, reverse digits of an integer. Example 1:                              ...

  9. [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 ...

随机推荐

  1. window.open的小技巧分享(转)

    今天再次谈起window.open是因为发现了一个比较好玩的小技巧,详细内容我们稍后详细说明.       聊到window.open,不得不说明一下他的使用方法,主要有两种形式:   window. ...

  2. myeclipse egit 安装失败 org.eclipse.e4.ui.css.swt.theme 0.0.0

    [前言] 首先确保您可能会被安装在阅读本文之前,myeclipse egit, 见文章:http://blog.csdn.net/uikoo9/article/details/17247405 事实上 ...

  3. M、V、C

    概述 Model-View-Controller(MVC),即模型-视图-控制器. MVC将软件系统分成三大部分:Model,View,Controller,三个部分通过某种机制通信 M.V.C的职能 ...

  4. WebAPI上传大文件

    今天在研究WebAPI的上传与下载,作为Rest的框架,更多是面向资源,就其本身来说,是不会涉及也不应该涉及到大文件的处理,具体多大呢,也就是ASP.NET的限值2G. ASP.NET的pipelin ...

  5. 前端插件@user

    分享一个 @user 前端插件   开源地址:https://github.com/yuezhongxin/Mention.js 插件效果:类似于微博或 github 中 @user 列表效果. 这是 ...

  6. 浙江大学PAT考试1069~1072(2013-11-2)

    11 题目地址:http://pat.zju.edu.cn/contests/pat-a-practise 1069: 由下降序和上升序两个四位数不断相减,然后得到新数据,始终会到达一个数字终止. 递 ...

  7. 我的Linux学习历程:那些我看过的Linux书籍们

    [+]查看原图http://www.ituring.com.cn/article/119401 来北京工作已经一个多月,大都市的生活比起读大学要忙碌得多,尤其是出行,基本以小时为基本的计时单位.有时茫 ...

  8. bash shell:重定向标准错误输出

    如何重定向标准错误输出到标准输出?如何把标准错误输出输出到一个文件? Bash提供了I/O重定向工具,有3个缺省的文件(标准输出流): stdin - 用来获取输入,比如键盘.文件重定向 stdout ...

  9. java学习笔记2——Eclipse的安装及汉化图解

    Eclipse的安装 有了JDK,你可以编译Java源码,运行Java程序,但是还没有代码编辑器,没有版本管理工具,也不能方便的管理工程文件,不能与团队协作.安装Eclipse,你才能完成这些工作. ...

  10. 《java系统性能调优》--1.发现瓶颈

    性能啊!性能! 之所以想写写性能调优,也是有感于我们的项目,我们採用一些手段使得系统性能上升了一个台阶,总是须要把这点经验沉淀一下.随着工作的深入,关于系统性能的事肯定还有非常多,也算是通过这个系列文 ...