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. POJ_1284 Primitive Roots 【原根性质+欧拉函数运用】

    一.题目 We say that integer x, 0 < x < p, is a primitive root modulo odd prime p if and only if t ...

  2. zoj4062 Plants vs. Zombies 二分+模拟(贪心的思维)

    题目传送门 题目大意:有n个植物排成一排,标号为1-n,每株植物有自己的生长速度ai,每对植物浇一次水,该株植物就长高ai,现在机器人从第0个格子出发,每次走一步,不能停留,每一步浇一次水,总共可以走 ...

  3. Tarjan算法打包总结(求强连通分量、割点和Tarjan-LCA)

    目录 Tarjan打包总结(求强连通分量.割点和Tarjan-LCA) 强连通分量&缩点 原理 伪代码 板子(C++) 割点 原理 伪代码 最近公共祖先(LCA) 原理 伪代码 板子 Tarj ...

  4. 解决linux一段时间不操作失去连接的问题

    解决mac下ssh空闲一段时间自动断开的问题 http://www.haorooms.com/post/mac_iterm2_ssh 问题现象 用 ssh 命令连接服务器之后,如果一段时间不操作,再次 ...

  5. Python入门笔记——(1)数字与表达式

    一.算术运算 整除:// 取余:% 乘方:** (a ** b = pow(a, b)) 十六进制表示:0x...,八进制表示0... round(x [, n]):对x从小数点第n位取四舍五入结果, ...

  6. ngin日志格式

    日志格式 为了更好满足分析场景,推荐采用如下log_format配置.   log_format main '$remote_addr - $remote_user [$time_local] &qu ...

  7. Jmeter4.0----HTTP Cookie管理器(9)

    1.说明 在脚本编写的过程中,我们常常会遇到用户登录之后的相关操作,但是又不想去通过脚本先模拟用户登录,再使用cookie值保持登录,做后续的操作的情况下,我们就会用到HTTP Cookie管理. H ...

  8. (转)Linux网络状态工具ss命令使用详解

    Linux网络状态工具ss命令使用详解 原文:http://www.landui.com/help/show-5991.html ss 是 socket statistics 的缩写.顾名思义,ss ...

  9. vue懒加载 && 浏览器高度

    当我们进入首页时,可能有很多条目需要显示,但是如果条目太多,我们全部将之显示出来就会造成性能的消耗,比如,我在第一条就找到了需要的或者我就看前面两条我就不想看后面的了,所以,这时候如果使用全部加载的方 ...

  10. 九度oj题目1027:欧拉回路

    题目1027:欧拉回路 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2844 解决:1432 题目描述:     欧拉回路是指不令笔离开纸面,可画过图中每条边仅一次,且可以回到起点的一条 ...