[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 ...
随机推荐
- sql中group by
某图书馆收藏有书籍具有不同的出版年份,管理员需要做一下统计工作: (1)每一年书籍的数目,如: 2000年有10本书, 2001年有5本书... (2)每一种书籍的数目,如: 西游记有10本, 三国演 ...
- focus,focusin,blur,focusout区别
focus与focusin 1.共同点:当 <div> 元素或其任意子元素获得焦点时执行事件 2.区别:focus不支持冒泡,而focusin支持冒泡: blur与focusout 1.共 ...
- Java应用异常状态监测
阿里巴巴中间件技术专栏 老板最近分派了一个任务,说线上客户在部署应用的时候发生了系统级别的OOM,触发了OOM Killer杀掉了应用,让我们解决这个问题. 对于这个任务,我从如下几点开始调研.分析与 ...
- Linux中的FTP服务
FTP服务 文件传输协议 FTPFile Transfer Protocol 早期的三个应用级协议之一 基于C/S结构 双通道协议:数据和命令连接 数据传输格式:二进制(默认)和文本 两种模式:服务器 ...
- java io-----转
https://blog.csdn.net/zch19960629/article/details/77917739 输入输出的重要性: 输入和输出功能是Java对程序处理数据能力的提高,Ja ...
- [NOI2001] 炮兵阵地 (状压Dp经典例题)
如果您的电脑比较优秀能在 1sec 内跑过 2^1000 的时间复杂度,不妨你可以尝试一下,其实实际时间复杂度远远少于 2^1000,作为骗分不错的选择QAQ,然后我们来分析一下正解: 很显然此题是一 ...
- pip安装requests库失败
pip install 安装第三方插件是出现Could not fetch URL https://pypi.python.org/simple/pool/: There was a problem ...
- Python+selenium常用方法(Webdriver API)
小编整理了目前学习的Python+selenium常用的一些方法函数,以后有新增再随时更新. 加载浏览器驱动: webdriver.Firefox() 打开页面:get() 关闭浏览器:quit() ...
- python010 Python3 元组
Python3 元组Python 的元组与列表类似,不同之处在于元组的元素不能修改.元组使用小括号,列表使用方括号.元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可.如下实例: tup1 = ...
- [HNOI2004]宠物收养场(Treap)
洛谷传送门 这题真是恶心,一开始没理解题意. 原来如果有狗,狗就会存在收养场中,直到有人来领养: 如果有人,人也会存在收养场中,直到有狗来被领养. 就是建一个treap,狗来把狗插进去,人来后把狗领养 ...