这道题是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. django-form and fields validation

    参考资料 清除数据与表单验证 清除数据时会进行表单验证. 在表格处理时有三种clean方法可调用,通常是在对表单调用is_valid()时执行. clean响应:一般有两种结果,如果处理的数据有问题, ...

  2. 关于RegExp的一些使用的练习(代码加注释)

    <!DOCTYPE html> <html> <head> <title>title</title> <meta charset=&q ...

  3. Vue.js - day7

    使用mui的tab-top-webview-main完成分类滑动栏 兼容问题 和 App.vue 中的 router-link 身上的类名 mui-tab-item 存在兼容性问题,导致tab栏失效, ...

  4. SIT&UAT

  5. 中国区 Azure 应用程序开发说明

    1.文档简介 微软公司为其在境外由微软运营的 Azure 服务(以下简称为 “境外 Azure”),创建和部署云应用程序,提供了相应工具. 在中国,由世纪互联运营的 Microsoft Azure ( ...

  6. vue.js与react.js相比较的优势

    vue.js的简介 vue.js是一个javascript mvvm库,它是以数据驱动和组件化的思想构建的.我们平时多用js去操作dom,vue.js则是使用了数据绑定驱动来操作dom的,也就是说创建 ...

  7. tomcat+nginx 横向扩展

    1.分别在电脑上部署两个tomcat tomcat1  tomcat2 2.不是nginx 并启动 输入 localhost 并进入nginx欢迎界面,证明部署成功 3.修改nginx 配置 ngin ...

  8. FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed for Block pool <registering> (Datanode Uuid unassigned) service to controller/192.168.1.183:9000. Exiting. java.io.IOExcep

    2018-01-09 09:47:38,297 FATAL org.apache.hadoop.hdfs.server.datanode.DataNode: Initialization failed ...

  9. vs和github同步开发步骤

    首先,这是在visual studio中使用.需要了解关于vs同步github必不可少.下载安装破解什么的先完成vs. 1. 然后安装一个vs中使用github的插件.vs自带的下载.这个是下载地址. ...

  10. C++值传递、引用传递和指针传递

    #include<iostream> using namespace std; //值传递 void change1(int n){ cout<<"值传递--函数操作 ...