leetcode之旅(10)-Roman to Integer
题目描述:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
Subscribe to see which companies asked this question
预备知识:
记数方法
基本字符
I
V
X
L
C
D
M
相应的阿拉伯数字表示为
1
5
10
50
100
500
1000
1、相同的数字连写,所表示的数等于这些数字相加得到的数,如:Ⅲ = 3;
2、小的数字在大的数字的右边,所表示的数等于这些数字相加得到的数, 如:Ⅷ = 8;Ⅻ = 12;
3、小的数字,(限于Ⅰ、X 和C)在大的数字的左边,所表示的数等于大数减小数得到的数,如:Ⅳ= 4;Ⅸ= 9;
4、正常使用时,连写的数字重复不得超过三次。(表盘上的四点钟“IIII”例外)
5、在一个数的上面画一条横线,表示这个数扩大1000倍。 ##
思路:
一个罗马字符对应一个数字权,那么就可以用数据结构map
对于s中出现的每一个罗马字符从右到左(无低位高位这么一说),如果权值在变大,则权相加
如果变小了,则减去这个权值 ##
代码:
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int romanToInt(String s) {
int result=0;
Map<Character,Integer> roman = new HashMap<Character,Integer>();
roman.put('I', 1);
roman.put('V', 5);
roman.put('X', 10);
roman.put('L', 50);
roman.put('C', 100);
roman.put('D', 500);
roman.put('M', 1000);
for(int i=s.length()-1;i>=0;i--)
{
if(i==s.length()-1)
{
result=roman.get(s.charAt(i));
continue;
}
if(roman.get(s.charAt(i)) >= roman.get(s.charAt(i+1)))
result+=roman.get(s.charAt(i));
else
result-=roman.get(s.charAt(i));
}
return result;
}
}
leetcode之旅(10)-Roman to Integer的更多相关文章
- leetcode之旅(11)-Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 【LeetCode算法-13】Roman to Integer
LeetCode第13题 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symb ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- leetcode第13题--Roman to Integer
Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- [LeetCode&Python] Problem 13. Roman to Integer
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户.需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式. Given a roman numeral, convert it ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- [LeetCode][Python]Roman to Integer
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/roman-t ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- Android简易实战教程--第十三话《短信备份和还原~三》
之前写过短信备份的小案例,哪里仅仅是虚拟了几条短信信息.本篇封装一个业务类,且直接通过内容提供者,访问本系统的短信信息,再提供对外接口.如果想要短信备份和短信还原,直接复制这段代码即可.对于您调用这个 ...
- Cocos2D结合CoreGraphics实现RPG人物中空黑洞吸入效果
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 之前的博文中我们实现了RPG人物的复古效果. 现在我们再完点h ...
- scala学习笔记1(表达式)
<pre name="code" class="plain">//Scala中的 main 函数需要存在于 object 对象中,我们需要一个obj ...
- [GitHub]第八讲:GitHub Pages
Github Pages 是 github 公司提供的免费的静态网站托管服务,用起来方便而且功能强大,不仅没有空间限制,还可以绑定自己的域名.在 https://pages.github.com/ 首 ...
- Android官方命令深入分析之bmgr
作者:宋志辉 bmgr是一个可以跟Backup Manager进行交互的shell工具,要使用这个工具,Android设备API最小为8.它提供了备份和恢复操作的命令,所以你无需频繁的清除数据.这些命 ...
- Android项目-高考作文-AsyncTask的不足
1, AsyncTask的不足. 从android4.0开始, 后台只允许一个AsyncTask执行, 如果当前的AsyncTask没有执行完毕, 那么当前的请求一直处于等待状态. 直到上一个执行完毕 ...
- ROS_Kinetic_11 ROS程序基础Eclipse_C++(二)
ROS_Kinetic_11 ROS程序基础Eclipse_C++(二) 编写简单的Service和Client (C++): http://wiki.ros.org/cn/ROS/Tutorials ...
- 我眼中的Linux设备树(二 节点)
二 节点(node)的表示首先说节点的表示方法,除了根节点只用一个斜杠"/"表示外,其他节点的表示形式如"node-name@unit-address".@前边 ...
- ROS_Kinetic_05 ROS基础内容(二)
ROS_Kinetic_05 ROS基础内容(二) 1. ROS节点node 官网教程:http://wiki.ros.org/cn/ROS/Tutorials/UnderstandingNodes ...
- Socket编程实践(3) --Socket API
socket函数 #include <sys/types.h> #include <sys/socket.h> int socket(int domain, int type, ...