题目:

Given a roman numeral, convert it to an integer.

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

题意:

给定一个罗马数字,将其转化为整数。

给定的输入保证在1-3999之间

算法分析:

* 罗马数字规则:

* 1, 罗马数字共同拥有7个,即I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和M(1000)。

* 罗马数字中没有“0”。

* 2, 反复次数:一个罗马数字最多反复3次。

* 3, 右加左减:

* 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。

* 在较大的罗马数字的左边记上较小的罗马数字。表示大数字减小数字。

AC代码:

public class Solution
{
private int sss; public int romanToInt(String s)
{
Map<Character, Integer> dct=new HashMap<Character, Integer>() ;
dct.put('I', 1);
dct.put('i', 1);
dct.put('V', 5);
dct.put('v', 5);
dct.put('X', 10);
dct.put('x', 10);
dct.put('L', 50);
dct.put('l', 50);
dct.put('C', 100);
dct.put('c', 100);
dct.put('D', 500);
dct.put('d', 500);
dct.put('M', 1000);
dct.put('m', 1000);
int sum = 0, j;
for(int i = 0; i < s.length(); ++i)
{
j = i+1;
if(j < s.length() && dct.get(s.charAt(j)) > dct.get(s.charAt(i)))
{
sum += dct.get(s.charAt(j)) - dct.get(s.charAt(i));
i = j;
}
else
sum += dct.get(s.charAt(i));
}
return sum;
}
}

[LeetCode][Java] Roman to Integer的更多相关文章

  1. [LeetCode][Python]Roman to Integer

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-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. 【LeetCode】Roman to Integer & Integer to Roman

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  4. leetcode:Roman to Integer and Integer to Roman

    2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...

  5. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  6. 【JAVA、C++】LeetCode 013 Roman to Integer

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

  7. Java [leetcode 13] Roman to Integer

    问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...

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

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

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

随机推荐

  1. Ext 6.5.3 classic版本,自定义实现togglefield开关控件

    1,在Ext 6.5.3的classic版中没有提供开关控件,参照modern版中 togglefield开关的实现,继承滑动器(sliderfield),自定义一个开关按钮.支持value绑定和点击 ...

  2. How far away?

    C - How far away ? HDU - 2586 There are n houses in the village and some bidirectional roads connect ...

  3. Centos7中yum安装jdk及配置环境变量

    系统版本 [root@localhost ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) #安装之前先查看一下有无系统 ...

  4. docker 阿里云镜像加速器

    传送门:阿里云镜像地址 Ubuntu/CentOS 安装/升级你的Docker客户端 推荐安装1..0以上版本的Docker客户端,参考文档 docker-ce 如何配置镜像加速器 针对Docker客 ...

  5. 条款16:成对使用new和delete时要采取相同形式

    NOTE: 1.如果你在new表达式中使用[],必须在相应的delete表达式中也使用[].如果你在new表达式中不使用[],一定不要在相应的delete表达式中使用[].

  6. Python中的函数(4)

    一.传递列表 你经常会发现,向函数传递列表很有用,这种列表包含的可能是名字.数字或者更复杂的对象(如字典). 将列表传递给函数后,函数就能直接访问其内容. 栗子:假设有一个用户列表,我们要和其中每一位 ...

  7. 【笔记】mysql入门语句8条

    1.连接到数据库服务器 mysql -h host -uroot -pXXXX 2.查看所有库 show databases; 3.选库 use 库名 4.查看库下面的表 show tables; 5 ...

  8. POJ 1287 Networking (最小生成树模板题)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  9. 【HDU 6153】A Secret (KMP)

    Problem Description Today is the birthday of SF,so VS gives two strings S1,S2 to SF as a present,whi ...

  10. NOI模拟赛(3.15) sequence(序列)

    Description 小A有N个正整数,紧接着,他打算依次在黑板上写下这N个数.对于每一个数,他可以决定将这个数写在当前数列的最左边或最右边.现在他想知道,他写下的数列的可能的最长严格上升子序列(可 ...