Given a roman numeral, convert it to an integer.

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

将罗马数字转成阿拉伯数字。需要了解两者对应关系。

罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。
1、重复数次:一个罗马数字重复几次,就表示这个数的几倍。

2、
2.1 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。
2.2 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。

利用规则,可以将字符串对应字符转成对应的数字,存放在数组中,然后将数组中数字相加,相加的时候要满足2.2,即当后一个元素大于前一个元素,表示后一个元素减去前一个元素,也就是和减去前一个元素,加上后一个元素。见代码

tag:Math,String

class Solution {
public int romanToInt(String s) {
int[] a=new int[s.length()];
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
if(ch=='I') {a[i]=1;continue;}
if(ch=='V') {a[i]=5;continue;}
if(ch=='X') {a[i]=10;continue;}
if(ch=='L') {a[i]=50;continue;}
if(ch=='C') {a[i]=100;continue;}
if(ch=='D') {a[i]=500;continue;}
if(ch=='M') {a[i]=1000;continue;}
} int sum=0;
for(int i=0;i<a.length-1;i++){
if(a[i]<a[i+1])
sum-=a[i];
else
sum+=a[i];
} return sum+a[a.length-1]; //这里因为上面的循环没有操作最后一个数,所以加上最后一个数
}
}

Roman to Integer(将罗马数字转成整数)的更多相关文章

  1. [Leetcode] Roman to integer 罗马数字转成整数

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

  2. 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 ,即 ...

  3. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  4. 13. Roman to Integer[E]罗马数字转整数

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

  5. [Leetcode] String to integer atoi 字符串转换成整数

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  6. [LeetCode] Roman to Integer 罗马数字转化成整数

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

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

  8. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  9. 8. String to Integer (atoi) 字符串转成整数

    [抄题]: Input: "42" Output: 42 Example 2: Input: " -42" Output: -42 Explanation: T ...

随机推荐

  1. Tomcat 5.5 JNDI Resource 配置 (tomcat数据源配置)

    转自:http://blog.csdn.net/fenglibing/article/details/4528512 Tomcat 5.5 JNDI Resource 配置 Author Blog:h ...

  2. 学习TensorFlow,多层卷积神经网络

    一.网络结构 二.代码 from tensorflow.examples.tutorials.mnist import input_data mnist = input_data.read_data_ ...

  3. Swift中实现Observable机制

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/51917539 ...

  4. ECMAScript 6之Set和Map数据结构

    Set 基本用法 ES6提供了新的数据结构Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身是一个构造函数,用来生成Set数据结构. var s = new Set(); [2, ...

  5. 【游戏周边】Unity,UDK,Unreal Engine4或者CryENGINE——我应该选择哪一个游戏引擎

    在digital-tutors看到的一篇文章,很多初学者都有这样的疑问,因此翻译到这里. 原文:http://blog.digitaltutors.com/unity-udk-cryengine-ga ...

  6. Chapter 3 Protecting the Data(2):分配列级权限

    原文出处:http://blog.csdn.net/dba_huangzj/article/details/39577861,专题目录:http://blog.csdn.net/dba_huangzj ...

  7. Chipmunk Rigid Bodies:cpBody

    Chipmunk刚体支持3种不同的类型: Dynamic(动态),Static(静态)以及Kinematic(混合态)刚体.它们拥有不同的行为和性能特征. 动态刚体是默认的刚体类型.它们可以对碰撞做出 ...

  8. ISLR系列:(2)分类 Logistic Regression & LDA & QDA & KNN

       Classification 此博文是 An Introduction to Statistical Learning with Applications in R 的系列读书笔记,作为本人的一 ...

  9. Leetcode_104_Maximum Depth of Binary Tree

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964475 Maximum Depth of Binar ...

  10. wing带你玩转自定义view系列(3)模仿微信下拉眼睛

    发现了爱神的自定义view系列,我只想说一个字:凸(艹皿艹 ) !!相见恨晚啊,早看到就不会走这么多弯路了 另外相比之下我这完全是小儿科..所以不说了,这篇是本系列完结篇....我要从零开始跟随爱哥脚 ...