这道题是LeetCode里的第13道题。

题目说明:

罗马数字包含以下七种字符: I, V, X, LCD 和 M

字符          数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做  XXVII, 即为 XX + V + II 。

通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:

  • I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
  • X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
  • C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。

给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。

示例 1:

输入: "III"
输出: 3

示例 2:

输入: "IV"
输出: 4

示例 3:

输入: "IX"
输出: 9

示例 4:

输入: "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.

示例 5:

输入: "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.

看完题目后,我就想到了我曾经学过的“左减右加”。就是从右边开始看,如果左边的比右边的小,那么就是减,否则加。

示例5就是个例子。C在M的左边,且C小于M,则为1000-100=900。

按照这个思路设计起来就很容易了

代码如下:

class Solution {
public:
int addorminus(int cur, int pre) {
if (pre > cur)return -1;
return 1;
}
int romanToInt(string s) {
char c;
int res = 0, pre = 0;
for (int i = s.length() - 1; i >= 0; i--) {
c = s[i];
switch (c) {
case 'I':res = res + addorminus(1, pre) * 1; pre = 1; break;
case 'V':res = res + addorminus(5, pre) * 5; pre = 5; break;
case 'X':res = res + addorminus(10, pre) * 10; pre = 10; break;
case 'L':res = res + addorminus(50, pre) * 50; pre = 50; break;
case 'C':res = res + addorminus(100, pre) * 100; pre = 100; break;
case 'D':res = res + addorminus(500, pre) * 500; pre = 500; break;
case 'M':res = res + addorminus(1000, pre) * 1000; pre = 1000; break;
}
}
return res;
}
};

运行结果:

44ms???不行,我得看看最快的是多少。

static const auto io_speed_up= [](){
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
return 0;
}();
class Solution {
public:
int romanToInt(string s) {
int len=s.size();
int sum=0;
for(int i=0;i<len;i++){
if(s[i]=='I'){
if(i+1<len&&s[i+1]=='V') {sum=sum+4;i++;}
else if(i+1<len&&s[i+1]=='X') {sum=sum+9;i++;}
else sum=sum+1;
}else if(s[i]=='X'){
if(i+1<len&&s[i+1]=='L') {sum=sum+40;i++;}
else if(i+1<len&&s[i+1]=='C') {sum=sum+90;i++;}
else sum=sum+10;
}else if(s[i]=='C'){
if(i+1<len&&s[i+1]=='D') {sum=sum+400;i++;}
else if(i+1<len&&s[i+1]=='M') {sum=sum+900;i++;}
else sum=sum+100;
}else if(s[i]=='V'){
sum=sum+5;
}else if(s[i]=='L'){
sum=sum+50;
}else if(s[i]=='D'){
sum=sum+500;
}else if(s[i]=='M'){
sum=sum+1000;
}
}
return sum;
}
};

代码来源:Click

好像确实更简单点,只是代码复杂了。还有开头的几行代码。

static const auto io_speed_up= [](){

    std::ios::sync_with_stdio(false);

    cin.tie(nullptr);

    return 0;

}();

这什么玩意?都没见过啊?io_speed_up?什么黑科技?找了找资料,先贴出链接,以后再看,现在看的不是很懂。按照我的理解就是一种增快读写的一种方式。难怪之前我写过一些代码体感就是printf和scanf要比cout和cin快。学习了学习了。Click

个人收获:

std::ios::sync_with_stdio(false);cin.tie(nullptr);这两行代码可以提高读写速度,缩短时间。题目本身不难,左减右加就完事了。

【LeetCode】Roman to Integer(罗马数字转整数)的更多相关文章

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

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

  2. [LeetCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  3. [Leetcode] Roman to integer 罗马数字转成整数

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

  4. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

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

  6. 013 Roman to Integer 罗马数字转整数

    给定一个罗马数字,将其转换成整数. 返回的结果要求在 1 到 3999 的范围内. 详见:https://leetcode.com/problems/roman-to-integer/descript ...

  7. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  8. LeetCode:Roman to Integer,Integer to Roman

    首先简单介绍一下罗马数字,一下摘自维基百科 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000).按照下述的规则可以表示任意正整数.需要注意的是罗 ...

  9. LeetCode: Roman to Integer 解题报告

    Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...

随机推荐

  1. bootstrap警告框、进度条和列表组

    警告框   <div class="container">      <div class="alert alert-success" rol ...

  2. table表格字母无法换行

    在项目中,用到的table比较多,本来布局挺好的,后来在td内写入英文字母,整个布局就乱了,会撑的很宽,不换行,后来才知道:一般字母的话会被浏览器默认是一个字符串或者说一个单词,所以不会自动换行. 于 ...

  3. IT之家学院:使用CMD命令行满速下载百度云

    转自:https://www.toutiao.com/a6545305189685920259/?tt_from=android_share&utm_campaign=client_share ...

  4. Linux配置临时IP地址

    # ifconfig 查看网卡信息,如下图所示: # ifconfig eth0 192.168.0.107 eth0表示第一块网卡,Linux中所有的设配都是文件,所以eth0是第一块网卡的文件名, ...

  5. css3 省略号

    overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 也无需给元素设置固定宽度!

  6. SublimeREPL配置Python3开发

    首先什么是REPL? A Read-Eval-Print-Loop (REPL) is available both as a standalone program and easily includ ...

  7. Codeforces Round #316 (Div. 2) C Replacement 扫描法

    先扫描一遍得到每个位置向后连续的'.'的长度,包含自身,然后在扫一遍求出初始的合并次数. 对于询问,只要对应位置判断一下是不是'.',以及周围的情况. #include<bits/stdc++. ...

  8. layui 数据table隐藏表头

    $('.layui-table .layui-table-cell > span').css({'font-weight': 'bold'});//表头字体样式 /*$('th').css({' ...

  9. 一个典型的flex布局,兼容性比较好

    html 代码: <body class="flex-wrap col-flex"> <header class="midCenter flex-wra ...

  10. angstromctf -No libc for You

    0x00 syscall syscall函数原型为: int syscall(int number, ...) 其中number是系统调用号,number后面应顺序接上该系统调用的所有参数.大概意思是 ...