【leetcode刷题笔记】Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字;否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字。利用一个HashMap存放罗马字符和数字的对应。
罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.com/sunshineatnoon/p/3856057.html
例如罗马数字DCXIX:500+100+10+1+10-2 = 619
代码如下:
public class Solution {
public int romanToInt(String s) {
if(s == null || s.length() == 0)
return 0;
HashMap<Character, Integer> map= new HashMap<Character,Integer>();
map.put('I', 1);
map.put('V', 5);
map.put('X', 10);
map.put('L', 50);
map.put('C', 100);
map.put('D', 500);
map.put('M', 1000);
int length = s.length();
int result = map.get(s.charAt(0));
int last = result;
for(int i = 1;i < length;i++){
int temp = map.get(s.charAt(i));
if(temp <= last)
result += temp;
else
result = result + temp - 2*last;
last = temp;
}
return result;
}
}
【leetcode刷题笔记】Roman to Integer的更多相关文章
- 【leetcode刷题笔记】Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题:设定一个变量 ...
- LeetCode刷题笔记和想法(C++)
主要用于记录在LeetCode刷题的过程中学习到的一些思想和自己的想法,希望通过leetcode提升自己的编程素养 :p 高效leetcode刷题小诀窍(这只是目前对我自己而言的小方法,之后会根据自己 ...
- 18.9.10 LeetCode刷题笔记
本人算法还是比较菜的,因此大部分在刷基础题,高手勿喷 选择Python进行刷题,因为坑少,所以不太想用CPP: 1.买股票的最佳时期2 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. ...
- LeetCode刷题笔记 - 12. 整数转罗马数字
学好算法很重要,然后要学好算法,大量的练习是必不可少的,LeetCode是我经常去的一个刷题网站,上面的题目非常详细,各个标签的题目都有,可以整体练习,本公众号后续会带大家做一做上面的算法题. 官方链 ...
- 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刷题笔记(双指针)
1.何为双指针 双指针主要用来遍历数组,两个指针指向不同的元素,从而协同完成任务.我们也可以类比这个概念,推广到多个数组的多个指针. 若两个指针指向同一数组,遍历方向相同且不会相交,可以称之为滑动窗口 ...
- 【leetcode刷题笔记】Integer to Roman
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【leetcode刷题笔记】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- LeetCode刷题笔记(1-9)
LeetCode1-9 本文更多是作为一个习题笔记,没有太多讲解 1.两数之和 题目请点击链接 ↑ 最先想到暴力解法,直接双循环,但是这样复杂度为n平方 public int[] twoSum(int ...
随机推荐
- 简洁的一键SSH脚本
这里发一个自己图省事搞的一个批量打通SSH的脚本,可能对于好多朋友也是实用的,是expect+python的一个组合实现,原理非常easy, 使用起来也不复杂,在此还是简单贴出来说说. noscp.e ...
- 卸载系统自带libevent
rpm -qa|grep libevent yum remove libevent* 或 rpm -e --nodeps --allmatches libevent*
- spring bean的scope
scope用来声明容器中的对象所应该处的限定场景或者说该对象的存活时间,即容器在对象进入其相应的scope之前,生成并装配这些对象,在该对象不再处于这些scope的限定之后,容器通常会销毁这些对象. ...
- Android系统移植与调试之------->如何修改Android默认字体大小和设置里面字体大小比例
因为我修改 ro.sf.lcd_density的值,将它从160修改 为120,所以导致整个系统的字体都变得很小.因此需要将整个字体变大,并且在设置-->显示-->字体大小的4个选项的值都 ...
- Jquery禁用所有checkbox
$("input[type=checkbox]").each(function(){ $(this).attr("disabled",false);});
- python学习-3.一些常用模块用法
一.time.datetime 时间戳转化为元组 1 >>> time.localtime() 2 time.struct_time(tm_year=2016, tm_mon=8, ...
- vim python缩进等一些配置
VIM python下的一些关于缩进的设置: 第一步: 打开终端,在终端上输入vim ~/.vimrc,回车. 第二步: 添加下面的文段: set filetype=python au BufN ...
- oldboyshell编程扩展内容
oldboyshell编程扩展内容一.命令的优先级 命令分为: ==> alias ==> Compound Commands ==> function ==> build_ ...
- 33_为应用添加多个Activity与参数传递
1\ 2\ 3\ 4\ 2 3
- Chrome Extension 扩展程序 小白入门
Chrome Extension 扩展程序 前请说明:本文适用于之前从来没有接触过chrome extension扩展程序的同学~ 编写demo 创建项目文件夹chrome_ext_demo,在项目根 ...