[LeetCode][Java] Roman to Integer
题目:
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的更多相关文章
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- 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 ,即 ...
- 【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 ...
- leetcode:Roman to Integer and Integer to Roman
2015-06-03 罗马数字以前接触过I到VIII比较多,直到遇见这个题目才知道更详细.阿拉伯数字和罗马数字之间的转换最重的是了解罗马数字的规则. 罗马数字规则:(总结) 1, 罗马数字共有7个,即 ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【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 ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- [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 ...
随机推荐
- Map与对象关系的思考之P1563玩具谜题
P1563 玩具谜题 结论: map在一些情况有种"对象"的意味,在JSON中,对象可以用K-V格式存储:mybatis中参数是map或者对象都可以实现解析...k-v格式的数据存 ...
- 离散数学-集合的交并差集运算--STL-set类
代码其实很简单,我们只需要知道set类的使用方法就可以了,比如迭代器的定义( set<T>::iterator it=a.begin() ),和简单的insert函数插入,以及find函数 ...
- oracle函数笔记(1)
1.在数据库中显示当前时间:函数(sysdate) select sysdate from dual: 2.数据库中显示 :年/月/日 时/分/秒 ---函数:to_char(字段,'yyyy-mm- ...
- 前端面试绝对会考的JS问题!【已经开源】
写在前面 [前端指南]前端面试库已经开源,正在完善之中 [x] css问题 [x] html问题 [x] javascript问题 github地址 https://github.com/nanhup ...
- 牛客网 牛可乐发红包脱单ACM赛 A题 生成树
[题解] 其实就是求两棵树不同的边有多少条.那么我们用一个set来去重即可. #include<cstdio> #include<cstring> #include<se ...
- 构造MaxTree
链接:https://www.nowcoder.com/questionTerminal/a502c7c3c65e41fdaf65eec9e0654dcb 来源:牛客网 [编程题]构造MaxTree ...
- 3.3.3 使用 join 连接字段
join 命令可以将多个文件结合在一起,每个文件里的每条记录,都共享一个键值(key),键值指的是记录中的主字段,通常会是用户名称.个人姓氏.员工编号之类的数据.举例来说,两个文件,一个列出所 ...
- 【Codeforces 827B】High Load
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 树的最长链是一定会经过两个叶子节点的. 我们可以构造一棵树,让最后的最长链一定是由经过根节点的两条链组成. 然后让这两条链的长度尽可能短就好. ...
- intellij idea2016
注册server http://idea.imsxm.com
- step 1:begin to identify something in english(to becaome a baby again)
long long ago , i think if i want to improve my english especially computer english . i must do so m ...