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. Ubuntu系统安装WeChat

    安装: 1.sudo apt install snapd snapd-xdg-open 2.sudo snap install electronic-wechat 运行: electronic-wec ...

  2. [转]【NODE】用WS模块创建加密的WS服务(WSS)

    [From] https://luojia.me/2015/07/21/%E3%80%90node%E3%80%91%E7%94%A8ws%E6%A8%A1%E5%9D%97%E5%88%9B%E5% ...

  3. List<Type> 随机排序

    public List<T> GetRandomList<T>(List<T> inputList){ //Copy to a array T[] copyArra ...

  4. PIE SDK自定义滤波

    1.算法功能简介 自定义滤波可以自由设置滤波模板,对数据进行处理,自定义滤波器的一般规则要求: ( 1) 滤波器的大小应该是奇数,这样它才有一个中心,例如 3x3, 5x5 或者 7x7.有中心了,也 ...

  5. SQL Server Reporting Service(SSRS) 第七篇 常见错误汇总

    1. The current action cannot be completed. The user data source credentials do not meet the requirem ...

  6. webstorm预览时把浏览器地址localhost改成IP

    可以通过 File --> Setting,搜索 deployment 点击 + 号 然后输入一个名称,选择:Local or mounted folder,点击 OK 接下来选择你的本地项目路 ...

  7. mysqldump 命令使用

    常见选项:--all-databases, -A: 备份所有数据库--databases, -B: 用于备份多个数据库,如果没有该选项,mysqldump把第一个名字参数作为数据库名,后面的作为表名. ...

  8. JavaScript 浮点数及运算精度调整总结

    JavaScript 浮点数及运算精度调整总结 JavaScript 只有一种数字类型 Number,而且在Javascript中所有的数字都是以IEEE-754标准格式表示的.浮点数的精度问题不是J ...

  9. vue中watch的使用

    vue中watch的使用 vue中的watch是一个比较重要的概念,通过他我们可以检测data的变化,下面进行详细的介绍. watch定义方式如下: {[key: string]: string | ...

  10. cloudermanager安装时database connection出现Unexpected error. Unable to verify database connection(图文详解)

    不多说,直接上干货! http://www.aboutyun.com/forum.php?mod=viewthread&tid=20455&extra=&page=2 欢迎大家 ...