罗马数字是阿拉伯数字传入之前使用的一种数码。罗马数字采用七个罗马字母作数字、即Ⅰ(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. Java 查看死锁的方法

    那我们怎么确定一定是死锁呢?有两种方法. 1>使用JDK给我们的的工具JConsole,可以通过打开cmd然后输入jconsole打开. 1)连接到需要查看的进程.

  2. Windows 32 程序设计

    C语言版 开发语言:C语言 开发工具:Visual Studio 2015 目      标:使用C语言,直接调用Windows API,创建Windows程序. 参考图书:<Windows程序 ...

  3. 采访ServiceStack的项目领导Demis Bellot——第2部分(转)

    ServiceStack是一个开源的.支持.NET与Mono平台的REST Web Services框架.InfoQ有幸与Demis Bellot深入地讨论了这个项目.在这篇两部分报道的第2部分中,我 ...

  4. C++学习16 继承时的名字遮蔽

    如果派生类中的成员变量和基类中的成员变量重名,那么就会遮蔽从基类继承过来的成员变量.所谓遮蔽,就是使用新增的成员变量,而不使用继承来的. 成员函数也一样,如果函数名和参数签名都相同,就会造成遮蔽.如果 ...

  5. crm 4 UserHasRole

    //获取当前人员是否含有指定角色权限 function UserHasRole(roleName) { //get Current User Roles, oXml is an object var ...

  6. [ZOJ 3662] Math Magic (动态规划+状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...

  7. (转)WebApi自动生成在线文档WebApiTestClient

    原文链接:http://www.cnblogs.com/landeanfen/p/5210356.html 前言:这两天在整WebApi的服务,由于调用方是Android客户端,Android开发人员 ...

  8. maven与git

    ================================================== maven的作用 ======================================== ...

  9. gnu c语言中的?:的作用

    #include <stdio.h> #include <stdlib.h> char * test() { return "abc" ?: "f ...

  10. Rolling Cursor Invalidations with DBMS_STATS.AUTO_INVALIDATE (文档 ID 557661.1)

      Rolling Cursor Invalidations with DBMS_STATS.AUTO_INVALIDATE (文档 ID 557661.1) 转到底部 In this Documen ...