这道题是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. iis的网站发布

    1.打开IIS服务器,添加“新网站”,命名网站的名称.物理路径(存放index.aspx的文件路径).ip地址和端口:2.在已经添加的网站,启用“目录浏览”,“默认文档”设置将要打开的网页 注:(1) ...

  2. jmeter中通过jdbc方式连接mysql数据库的配置参考

    jmeter中通过jdbc方式连接mysql数据库的配置参考: Database URL=jdbc:mysql://ip:port/dbname?useUnicode=true&allowMu ...

  3. HDU 1398 Square Coins 平方硬币 (普通母函数,水)

    题意: 有17种硬币,每种的面值为编号的平方,比如 1,4,9,16.....给出一个数字,求组成这个面值有多少种组法? 思路: 用普通母函数解,主要做的就是模拟乘法,因为硬币是无限的,所以每个构造式 ...

  4. COGS 1710. [POJ2406]字符串的幂

    ★☆   输入文件:powerstrings.in   输出文件:powerstrings.out   简单对比时间限制:3 s   内存限制:256 MB [题目描述] 对于给定的两个字符串a,b, ...

  5. 洛谷 P2264 情书

    题目背景 一封好的情书需要撰写人全身心的投入.lin_toto同学看上了可爱的卡速米想对她表白,但却不知道自己写的情书是否能感动她,现在他带着情书请你来帮助他. 题目描述 为了帮助lin_toto,我 ...

  6. 仿天猫淘宝的ShopNC好商城原生Android 客户端源码项目

    开发环境:Android Studio 2.0 | Gradle 2.0.0最后更新:2016-04-28 简介:基于好商城V4的Android客户端 目前已完成的功能(概述): 1.启动页 -> ...

  7. (十二)maven之nexus仓库的基本用法

    nexus仓库的基本用法 ① 启动nexus. 上一章有提到:https://www.cnblogs.com/NYfor2018/p/9079068.html ② 访问http://localhost ...

  8. Stream.iterate方法与UnaryOperator

    前提:本人在翻看<Java核心技术II>的时候在p17的时候发现一段代码不是很明白.不知道为什么就输出了1,2,3,4,5,6,7,8,9,10,...也不知道n-n.add(BigInt ...

  9. 推荐一个免费的生成词云(word cloud)的在线工具

    "词云"这个概念由美国西北大学新闻学副教授.新媒体专业主任里奇·戈登(Rich Gordon)提出. "词云"就是对网络文本中出现频率较高的"关键词& ...

  10. PHP 递归无限极下级

    下面是自己用到的一些递归方法,当然都是借鉴的,各位看官请勿怪 第一种 有层级 $array = array( array('id' => 1, 'pid' => 0, 'n' => ...