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如何路由映射网址

    对于web容器来说,根据请求客户端路径路由到对应的资源属于其核心功能,假设用户在自己电脑上使用浏览器输入网址http://www.test.com/test/index.jsp,报文通过互联网网络到达 ...

  2. iOS中 基于LBXScan库二维码扫描 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 首先声明这个二维码扫描是借助于zxing. 功能模块都完全封装好了,不过界面合你口味,直接使用就好,如果不合口味,后 ...

  3. 银联在线 网关支付 (JAVA版)

    这一版本的编写是在我上一次博客的基础上写的,有不懂得童鞋可以先看下我的原先在线支付的博客,熟悉下:http://blog.csdn.net/yulei_qq/article/details/45197 ...

  4. findbugs, checkstyle, pmd的myeclipse7.5+插件安装(转:http://blog.csdn.net/priestmoon/article/details/63941)

    CheckStyle (1)下载net.sf.eclipsecs_5.3.0.201012121300-updatesite-.zip (2)打开MyEclipse,Help->Software ...

  5. AnimatedPathView实现自定义图片标签

    老早用过小红书app,对于他们客户端笔记这块的设计非常喜欢,恰好去年在小红书的竞争对手公司,公司基于产品的考虑和产品的发展,也需要将app社交化,于是在社区分享这块多多少少参照了小红书的设计,这里面就 ...

  6. C++异常处理基本思想

    传统错误处理机制 通过函数返回值来处理错误. 异常处理的基本思想 1)C++的异常处理机制使得异常的引发和异常的处理不必在同一个函数中,这样底层的函数可以着重解决具体问题,而不必过多的考虑异常的处理. ...

  7. python的list

    1.定义list >>> li = ["a", "b", "mpilgrim", "z", " ...

  8. Lucene 自动补全

    package com.pera.suggestion; import java.io.IOException; import java.io.Reader; import java.util.Arr ...

  9. Java进阶(九)正则表达式

    java正则表达式 序 由于项目中使用到了利用正则表达式进行表单的校验,回想一下正则表达式的内容,忘得也差不多了,俗话说:"温故而知新,可以为师矣".今天就简单的温故一下正则表达式 ...

  10. 纯命令提交代码到git仓库(教你怎么装逼)

    如果不喜欢用命令的请点链接:http://blog.csdn.net/xiangzhihong8/article/details/50715427 我这里用纯命令,主要是因为这两天不知道什么原因,ba ...