题目:

Given a roman numeral, convert it to an integer.

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

题目分析:

将罗马数字转换成阿拉伯数字,规则如下:罗马数字共有七个,即I(1),V(5),X(10),L(50),C(100),D(500),M(1000)。 按照下面的规则可以表示任意正整数。

重复数次:一个罗马数字重复几次,就表示这个数的几倍。
右加左减:在一个较大的罗马数字的右边记上一个较小的罗马数字,表示大数字加小数字。在一个较大的数字的左边记上一个较小的罗马数字,表示大数字减小数字。
但是,左减不能跨越等级。比如,99不可以用IC表示,用XCIX表示。

加线乘千:在一个罗马数字的上方加上一条横线或者在右下方写M,表示将这个数字乘以1000,即是原数的1000倍。同理,如果上方有两条横线,
 即是原数的1000000倍。

代码实现:

public static int romanToInt(String s) {

        char[] cs = s.toCharArray();// 变成char数组
int result = 0;
for (int i = 0; i < cs.length; i++) { if (i + 1 < cs.length) {
if (oneRomanToInt(cs[i]) < oneRomanToInt(cs[i + 1])) {// 规则:大的小的在前面为负数
result -= oneRomanToInt(cs[i]);
} else {
result += oneRomanToInt(cs[i]);
}
} else {// 把最后一个加上
result += oneRomanToInt(cs[i]);
} } return result;
} public static int oneRomanToInt(char c) {// 罗马数字转换
switch (c) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}

总体来说这个比较简单,但是要注意小数在大数前面是负数!!!

leetcode修炼之路——13. Roman to Integer的更多相关文章

  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 罗马数字转化成整数

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...

  9. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

随机推荐

  1. J2EE开源项目

    这篇文章写在我研究J2SE.J2EE近三年后.前3年我研究了J2SE的Swing.Applet.Net.RMI.Collections.IO.JNI……研究了J2EE的JDBC.Sevlet.JSP. ...

  2. MyEclipse右键new菜单项的设置 及 Eclipse中各种文件不能保存中文的问题

    有时候,myeclipse右键new的时候经常出现一些ejb等文件你懂的,很是恶心~~ Window --> Customize Perspective --> Submenus --&g ...

  3. 转:我终于离开了年薪30w的IT行业

    题目乍看起来有点故意惹人眼球的味道,但是对于我事实就是如此,暂且请君听我细细道来!      先自曝家门,我03年毕业,05年来深,06年买车子,06年底结婚,07年买了房子,09年生了儿子,妻子.房 ...

  4. List、Set、 数组等转字符串

    public class Test { public static void main(String[] args) { String str = ""; // list转字符串 ...

  5. 动态规划(斜率优化):BZOJ 1010 【HNOI2008】 玩具装箱

    玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 8218  Solved: 3233[Submit] Description P 教授要去 ...

  6. 网络流(最小费用最大流):POJ 2135 Farm Tour

    Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...

  7. HDOJ 3466 Proud Merchants

    Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...

  8. 动态规划——E (LIS())最长上升子序列

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  9. lightoj 1198 最大权重匹配

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1198 #include <cstdio> #include <cst ...

  10. 6th day

    时光荏苒,岁月如梭,2016年马上就要过去了,今天PHP的核心知识基本上已经讲完,后面就要开始学习数据库,做项目之类的,心情还真是有点小激动.总之,新年新气象,祝愿各位在新的一年里能够心想事成,好好学 ...