罗马数字是阿拉伯数字传入之前使用的一种数码。罗马数字采用七个罗马字母作数字、即Ⅰ(1)、X(10)、C(100)、M(1000)、V(5)、L(50)、D(500)。
记数的方法:
  1. 相同的数字连写,所表示的数等于这些数字相加得到的数,如 Ⅲ=3;
  2. 小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数,如 Ⅷ=8、Ⅻ=12;
  3. 小的数字(限于 Ⅰ、X 和 C)在大的数字的左边,所表示的数等于大数减小数得到的数,如 Ⅳ=4、Ⅸ=9;
  4. 在一个数的上面画一条横线,表示这个数增值 1,000 倍,如

     

    =5000。


思路:将罗马数字的每个字母按照顺序转换成整数存入数组中;然后按照上述规则操作数组,得到最后的结果。

代码如下:

 class Solution {
public:
int romanToInt(string s) {
int result = ;
int last = ;
vector<int> tag;
int n = s.length();
for(int i = ; i < n; ++i)
{
switch (s[i])
{
case 'I':
tag.push_back();
break;
case 'X':
tag.push_back();
break;
case 'C':
tag.push_back();
break;
case 'M':
tag.push_back();
break;
case 'V':
tag.push_back();
break;
case 'L':
tag.push_back();
break;
case 'D':
tag.push_back();
break;
}
result = result + tag[i];
}
for(int i = ; i < n-; ++i)
{
if(tag[i] == || tag[i] == || tag[i] == )
{
if(tag[i] < tag[i+])
{
result = result - * tag[i];
}
}
} return result;
}
};

leetcode 13的更多相关文章

  1. C#版 - Leetcode 13. 罗马数字转整数 - 题解

    C#版 - Leetcode 13. 罗马数字转整数 - 题解 Leetcode 13. Roman to Integer 在线提交: https://leetcode.com/problems/ro ...

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

  3. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  4. Java实现 LeetCode 13 罗马数字转整数

    13. 罗马数字转整数 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 ...

  5. LeetCode (13): 3Sum Closest

    https://leetcode.com/problems/3sum-closest/ [描述] Given an array S of n integers, find three integers ...

  6. 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 ,即 ...

  7. LeetCode——13. Roman to Integer

    一.题目链接:https://leetcode.com/problems/roman-to-integer/ 二.题目大意: 给定一个罗马数字,返回它的整数形式. 三.题解: 这道题与12题恰好相反, ...

  8. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

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

  10. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

随机推荐

  1. Porlet标准:JSR168/JSR286/WSRP(转载)

    From:http://www.iteye.com/topic/620213 Portlet标准主要是JSR168,JSR286和WSRP. JSR168因为比较早,所以大部分的Portal都支持这个 ...

  2. [ActionScript 3.0] AS3 绘制正四面体(线条)

    package { import flash.display.MovieClip; import flash.display.Sprite; import flash.events.Event; im ...

  3. bootstrap在jsp中怎么没有效果?

    页面顶部<!DOCTYPE html> 

  4. Heka–>Elasticsearch 索引数据过程的优化

    Heka 的参数配置跟Elasticsearch的参数没有关系,Heka只负责按照配置发送数据,所以索引的优化主要在 Elaticsearch端来完成. 下面是Elasticsearch的一些相关概念 ...

  5. Storm实时计算框架的编程模式

    storm分布式流式计算框架. nimbus:主进程服务(职责就是任务的分配的,程序的分发) supervisor:工作进程服务(职责就是启动线程池,接受任务,运行任务,报告任务的运行状态) 注意容错 ...

  6. ASP.Net软件工程师基础(一)

    本人目前是一名有1年左右ASP.Net开发经验的的软件开发工程师,目前公司用的是MVC+EF+...做的网站.写这套总结性系列文章的目的有两个:一是帮助自己总结一下自己到底有多少斤两,而不是一味的学新 ...

  7. Android 媒体键监听以及模拟媒体键盘的实现 demo

    有时我们需要程序模拟按钮或点击,而手机本身又没有,哪么可以采取其它方式 模拟实现,最后再去实际设备去测试(前期一般都拿不到设备): 如上一首,下一首,暂停等,手机上是没有的,但有些设备上是有的,所以我 ...

  8. [AFUI]App Framework Quickstart

    ---------------------------------------------------------------------------------------------------- ...

  9. 【LeetCode】6. ZigZag Conversion 锯齿形转换

    题目: 思路: 以图为例:s={'A','B','C','D','E','F','G','H'.....} 1.先不考虑中间元素F.G.H.N...,每一行前后元素在数组中对应下标相差size=2*n ...

  10. python基础set

    1.set set是一个无序的不重复的集合 li=[11,22,33,11] s=set(li) print(s) {11,22,33}  set提供的方法 1.add(self, *args, ** ...