直接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. Levenshtein Distance,判断字符串的相似性

    private int LevenshteinDistance(string s1,string s2,int maxValue) { if (s1 == null|| s1.Length == 0) ...

  2. 最大匹配字符串LCS,The Longest Common Substring

    public enum BackTracking { UP, LEFT, NEITHER, UP_AND_LEFT } public abstract class LCSBaseMatch { /// ...

  3. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  4. 【Python】【数据库】

    #[[数据库]]'''MySQL是Web世界中使用最广泛的数据库服务器.SQLite的特点是轻量级.可嵌入,但不能承受高并发访问,适合桌面和移动应用.而MySQL是为服务器端设计的数据库,能承受高并发 ...

  5. 基于 Python 和 Pandas 的数据分析(2) --- Pandas 基础

    在这个用 Python 和 Pandas 实现数据分析的教程中, 我们将明确一些 Pandas 基础知识. 加载到 Pandas Dataframe 的数据形式可以很多, 但是通常需要能形成行和列的数 ...

  6. 什么是可哈希的(hashable)

    如果一个对象在自己的生命周期中有一哈希值(hash value)是不可改变的,那么它就是可哈希的(hashable)的,因为这些数据结构内置了哈希值,每个可哈希的对象都内置了__hash__方法,所以 ...

  7. ArcFace 2.0 Demo [C++]

    环境: win10(10.0.16299.0)+ VS2017 sdk版本:ArcFace v2.0 OPENCV3.43版本 x64平台Debug.Release配置都已通过编译 下载地址:http ...

  8. nodejs + ts 配置

    参考:https://github.com/nestjs/typescript-starter 和 How to get auto restart and breakpoint support wit ...

  9. easyUI tree 自定义图标

    文章转载自: https://blog.csdn.net/zhlantian/article/details/52913115 近期由于项目中需要使用easyui tree树形列表,并在系统中动态配置 ...

  10. MySQL中如何建立主从复制

    ## 1 概述 ## MySQL内建的复制功能是构建大型,高性能应用程序的基础.将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves) ...