直接for循环,并且判断是否出现IV等情况

int which(char ch)
{
if(ch == 'I')
return ;
else if(ch == 'V')
return ;
else if(ch == 'X')
return ;
else if(ch == 'L')
return ;
else if(ch == 'C')
return ;
else if(ch == 'D')
return ;
else return ;
} class Solution {
public:
int romanToInt(string s) {
int flag = ;
int ans = ;
for(int i=; i<s.size(); i++)
{
if(i != s.size()- && which(s.at(i)) < which(s.at(i+)))
{
flag = ;
}
if(flag)
{
ans -= which(s.at(i));
flag = ;
}
else
ans += which(s.at(i));
}
cout << ans;
return ans;
}
};

13. Roman to Integer C++的更多相关文章

  1. 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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  4. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  5. 13. Roman to Integer【leetcode】

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  6. 【LeetCode】13. Roman to Integer (2 solutions)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  7. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  8. 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 ...

  9. 13. Roman to Integer[E]罗马数字转整数

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

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

随机推荐

  1. Linux 解决 firefox 中文页面乱码问题

    1.由于 firefox 默认是允许网页自己选择字体,在 Linux 上便会出现部分网站的乱码情况.因此可以取消允许页面自己选择字体这个选项便能解决部分乱码情况.

  2. Python打印矩形、直角三角形、等腰三角形、菱形

    思路如下: (1)先打印一个星号并换行 print("*") (2)打印一行6个星号 for i in range(6): print("*", end=&qu ...

  3. vscode "没有活动的源代码控制提供程序" 解决办法

    确保安装git F1,输入Show built-in Extensions, 左侧列表中找Git,然后启用即可

  4. 【NOIP 2016】Day2 T3 愤怒的小鸟

    Problem Description \(Kiana\) 最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 \((0,0)\) 处,每次 \(Kiana\ ...

  5. 解决UnicodeEncodeError: 'gbk' codec can't encode character u'\u25aa' in position 344 : illegal multiby

    Python拿来做爬虫的确很不错,但是字符串的编码的确是稍不留神就是一个坑,GBK编码和Unicode编码的转化出现问题也是很多的,今天在解析网页数据的时候出现上述错误,解决方案如下: one_str ...

  6. MySQL数据库自动备份

    1.vi /home/wangcn/auto_log.sh#!/bin/bash #Shell Command For Backup MySQL Database Everyday Automatic ...

  7. 串口.Qt532测试(异步)

    环境:Win7x64.Qt5.3.2 MSVC OpenGL(x86).vs2010(x86) 项目所在位置:E:\Project_Qt532\SeriesPort_Qt532_Z ZC:这里 同步方 ...

  8. [原]关于helios自定义面板简述

    想研究一下helios和自己仿真软件的适配,于是找了一下关于helios的使用 首先关于helios的使用有一个“vr2009”的发帖者有过很详细描述: http://www.insky.cn/bbs ...

  9. eclipse中配置maven和创建第一个 Spring Boot Application

    关于Maven的下载.环境变量的配置自行百度,今天记录一下在Eclipse中配置Maven的操作: mvn -v 出现上图说明maven和jdk的要求都达到了(jdk要8.0及以上的版本) 然后在ec ...

  10. 力扣(LeetCode) 20. 有效的括号

    给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...