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. HDU - 1403 后缀数组初步

    题意:求两个串的最长公共子串 两个串连接起来然后求高度数组 注意两个sa值必须分别在不同一侧 本题是用来测试模板的,回想起青岛那次翻车感觉很糟糕 #include<iostream> #i ...

  2. 1136 A Delayed Palindrome (20 分)

    Consider a positive integer N written in standard notation with k+1 digits a​i​​ as a​k​​⋯a​1​​a​0​​ ...

  3. Android 调整图标和字体大小

    1. Root 2. 进system,找到build.prop 3. 用RE管理器,编辑 ro.sf.lcd_density=320, 后面的数值随意调整,越大图标越大,不要太贪心,图标变大会显示不全 ...

  4. Tomcat(一)Tomcat常用配置

    操作系统:win8 Jdk版本:1.7.0_51 Jdk目录:C:\Program Files\Java\jdk1.7.0_51 Tomcat版本:8.0.3 Tomcat目录:D:\Program  ...

  5. jQuery遍历祖先元素:parentsUntil

    Description: Get the ancestors of each element in the current set of matched elements, up to but not ...

  6. php高手干货【必看】

    1.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量, 单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的"函数&quo ...

  7. 从XCodeGhost事件看软件来源鉴别的重要性

    事件 事件引爆于9月18日乌云网公布的一则分析报告:"XCode编译器里有鬼 – XCodeGhost样本分析",这份纯粹的技术分析报告引发中国iOS生态链的众多开发者的关注. 引 ...

  8. URAL 1252 ——Sorting the Tombstones——————【gcd的应用】

    Sorting the Tombstones Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I ...

  9. tomcat-dbcp数据库连接池配置以及使用时候的一些坑

    一.数据库连接池 开发的时候经常会需要对数据库进行一些操作,比如说常见的增删改查之类的,当数据量小的时候,可以直接进行操作,但是当数据量增多的时候,每一次连接以及释放数据库都会耗费一定的时间,这个时候 ...

  10. 7、侧边栏:Menu

    1.单个侧边栏 导航的代码在分析源码的时候已经分析过了,下面只看他的一些应用与方法. /* ---示例代码----*/ <ion-menu [content]="mycontent&q ...