Given a roman numeral, convert it to an integer.

Input is guaranteed to be within the range from 1 to 3999.

解题:

将字符形式的罗马数字,转化为整形。输入在1~3999之间。罗马数字的书写规范,请参见罗马数字_百度百科

本题的关键点在于,如何处理I、X、C三个数字放在大数左边是相减,放在大数右边是相加。

解法是,可以从输入字符串的末端开始,从右向左遍历字符串。对于出现的一般罗马字符,进行累加,当出现I、X、C时,判断当前累加值是否达到(>=)5、50、500。如果达到则为相减,如果未达到,则为相加。

原因是,单反需要相减,必定是在大数的左边,因此必定大数已经出现。

 class Solution {
public:
int romanToInt(string s) {
int len = s.length();
int sum = ; for (int i=len-; i>=; --i) {
if (s[i] == 'I')
sum += sum >= ? - : ; if (s[i] == 'V')
sum += ; if (s[i] == 'X')
sum += sum >= ? - : ; if (s[i] == 'L')
sum += ; if (s[i] == 'C')
sum += sum >= ? - : ; if (s[i] == 'D')
sum += ; if (s[i] == 'M')
sum += ;
} return sum;
}
};

【Leetcode】【Easy】Roman to Integer的更多相关文章

  1. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  2. 【LeetCode题意分析&解答】37. Sudoku Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...

  3. 【LeetCode题意分析&解答】35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  4. ACM金牌选手整理的【LeetCode刷题顺序】

    算法和数据结构知识点图 首先,了解算法和数据结构有哪些知识点,在后面的学习中有 大局观,对学习和刷题十分有帮助. 下面是我花了一天时间花的算法和数据结构的知识结构,大家可以看看. 后面是为大家 精心挑 ...

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

  6. LeetCode 13. 罗马数字转整数(Roman to Integer)

    13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符        数值  I           1  V  ...

  7. 【LeetCode算法题库】Day4:Regular Expression Matching & Container With Most Water & Integer to Roman

    [Q10] Given an input string (s) and a pattern (p), implement regular expression matching with suppor ...

  8. 【LeetCode算法题库】Day5:Roman to Integer & Longest Common Prefix & 3Sum

    [Q13] Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Valu ...

  9. 【leetcode刷题笔记】Roman to Integer

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

  10. 【leetcode刷题笔记】Integer to Roman

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

随机推荐

  1. hdu1711 Number Sequence kmp模板

    题目传送门 学习博客 学习了kmp算法,理解了算法思想,但还没有到能把这个思想用语言来描述出来. #include<bits/stdc++.h> #define clr(a,b) mems ...

  2. C++ GUI Qt4编程(12)-6.1FindFileDialog

    1. 主要介绍了QGridLayout, QHBoxLayout, QVBoxLayout3种布局管理器的使用方法. 2. 在linux中,继承自QDialog的对话框,没有最大化.最小化.关闭按钮, ...

  3. PIE SDK大气校正

    1. 算法功能简介 大气校正的目的消除大气对太阳和来自目标的辐射产生吸收和散射作用的 影响,从而获得目标反射率.辐射率.地表温度等真实物理模型参数.大多数情 况下,大气校正同时也是反演地物真实反射率的 ...

  4. How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12

    add following lines below "export PATH="$PATH:$HOME/.rvm/bin" # Add RVM to PATH for s ...

  5. Oracle命令整理

    1 常用命令 常用命令 1 sqlplus  scott/tiger@192.168.47.10:1521/orcl      后面不要加: sqlplus  sys/oracle  as sysdb ...

  6. Best HTTP

    http://blog.csdn.net/u012322710/article/details/52860747 Best HTTP (Pro)  这是一款很多公司都在用的网页插件,感觉确实不错,分P ...

  7. unity发布安卓lua路径不存在问题

    项目用的是xlua 采用自定义加载方式 使用File去读取路径下的文件,lua文件本来放在了StreamingAssets路径下 PC运行无问题,发布安卓后,居然提示路径不存在. 查了下资料后发现,F ...

  8. android studio 框架搭建:加入注解框架Annotations

    参考github上的demo,新建一个project后,会有一个位于app文件夹下的局部build.gradle文件和一个位于根目录project下的全局build.gradle文件,我们要修改的是局 ...

  9. Flask 编写http接口api及接口自动化测试

    片言 此文中代码都是笔者工作中源码,所以不会很完整,主要摘常见场景的api片段用以举例说明 另:此文主要针对自动化测试人员,尤其有python基础阅读更佳. 笔者使用 python3.6 + post ...

  10. 数据库mysql中编码自动生成

    call PrGetRuleCodeNoDate('Table_Name'); call PrGetRuleCode('Table_Name');