这道题是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. html5基础知识学习

    HTML5腾讯课堂--html5前端开发视频 https://ke.qq.com/course/89671 html5_video http://www.myexception.cn/jquery/2 ...

  2. Java 多个if 和多个else if 的区别

    int a=1; if(a==1){System.out.println("1");} if(a==2){System.out.println("2");} i ...

  3. Vue.js - day6

    注意: 有时候使用npm i node-sass -D装不上,这时候,就必须使用 cnpm i node-sass -D 在普通页面中使用render函数渲染组件 在webpack中配置.vue组件页 ...

  4. poj 2406 Power Strings 周期问题

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 48139   Accepted: 20040 D ...

  5. XML文件的解析和序列化

    序列化: private void createXml() { XmlSerializer serializer = Xml.newSerializer();// xml文件生成器 File file ...

  6. 关于搭建系统直播和Thinkphp的杂谈(持续更新)

    Q:Access denied for user 'root'@'localhost' 错误 A:第一种:配置文件中把数据库的用户名密码再改一遍,把runtime里的文件删除  第二种:修改syste ...

  7. Python3实现自动备份

    需求 将重要文件备份到指定目录,存档文件名称为“当前日期.zip”. 前提 1) Windows系统 2) Python 3以上版本 旗舰版 #!usr/bin/python # -*- coding ...

  8. nmon安装和使用介绍

    使用参考地址:百度中搜索 nmon 博客园 使用文档参考地址:http://nmon.sourceforge.net/pmwiki.php?n=Site.Documentation nmmon地址:h ...

  9. (十一)mybatis之映射器(select)

    映射器 映射器的主要元素有八种: 元素名称 描述 select 查询语句,可自定义参数 insert 插入语句,执行后返回插入的条数 update 更新语句,执行后返回更新的条数 delete 删除语 ...

  10. java中的堆与栈

    Java 中的堆和栈 Java把内存划分成两种:一种是栈内存,一种是堆内存. 在函数中定义的一些基本类型的变量和对象的引用变量都在函数的栈内存中分配 . 当在一段代码块定义一个变量时,Java就在栈中 ...