13. Roman to Integer【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.
public class Solution {
public int romanToInt(String s) {
if (s == null || s.length()==0) {
return 0;
}
Map<Character, Integer> m = new HashMap<Character, Integer>();
m.put('I', 1);
m.put('V', 5);
m.put('X', 10);
m.put('L', 50);
m.put('C', 100);
m.put('D', 500);
m.put('M', 1000);
int length = s.length();
int result = m.get(s.charAt(length - 1));
for (int i = length - 2; i >= 0; i--) {
if (m.get(s.charAt(i + 1)) <= m.get(s.charAt(i))) {
result += m.get(s.charAt(i));
} else {
result -= m.get(s.charAt(i));
}
}
return result;
}
}
// 方法二
public class Solution {
/**
* @param s Roman representation
* @return an integer
*/
public int romanToInt(String s) {
// Write your code here
int ans;
char[] sc = s.toCharArray();
ans = toInt(sc[0]); //0 special
for (int i = 1; i < s.length(); i++) {
ans += toInt(sc[i]);
if (toInt(sc[i - 1]) < toInt(sc[i])) {
ans -= toInt(sc[i - 1]) * 2;
}
}
return ans;
}
int toInt(char s) {
switch(s) {
case 'I':return 1;
case 'V':return 5;
case 'X':return 10;
case 'L':return 50;
case 'C':return 100;
case 'D':return 500;
case 'M':return 1000;
}
return 0;
}
}
13. Roman to Integer【leetcode】的更多相关文章
- 419. Roman to Integer【medium】
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- 【LeetCode】数学(共106题)
[2]Add Two Numbers (2018年12月23日,review) 链表的高精度加法. 题解:链表专题:https://www.cnblogs.com/zhangwanying/p/979 ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 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 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 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. 机器人的运动范围
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】386. Lexicographical Numbers 解题报告(Python)
[LeetCode]386. Lexicographical Numbers 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
随机推荐
- sizzle选择器的使用
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Bootstrap进度条
前面的话 在网页中,进度条的效果并不少见,比如一个评分系统,比如加载状态等,通过简单.灵活的进度条,可以为当前工作流程或动作提供实时反馈.本文将详细介绍Bootstrap进度条 基本样式 Bootst ...
- wget访问SOAP接口
SOAP协议主要是XML交互,所以其访问过程类似于这样: wget --header='Content-Type: text/xml;charset=utf-8' --post-data='<s ...
- win10下安装python
1. 在官网下载python:https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe 这里下载的是3.5.2版. 2. 双击exe ...
- Java学习之路----计算圆形的面积和周长
题目:计算圆形的面积,其中圆形的半径是随意指定. 源代码以及所有的分析思路都如下: import java.util.Scanner; //引入Scanner类 public class TestAr ...
- 《Java编程思想》第一二章
前段时间一直通过网络教程学习Java基础,把面向对象部分学完之后本来打算继续深入学习,但是感觉自己操之过急了,基础根本不够扎实,所以入手了一本<Java编程思想>,希望先把基础打好,再深入 ...
- Iterator对对象遍历
//实例对象tables List<Table> tables = new TableManager(getApplicationContext()).queryTables(); sp ...
- Java基础语法<十一> 异常 断言 日志 调试
1 处理错误 1.1 异常分类 Error类层次描述了Java运行时系统的内部错误和资源耗尽错误. 设计Java程序时,主要关注Exception层次结构. 由程序错误导致的异常属于RuntimeEx ...
- redhat系列linux系统 修改主机名的正确方法
##注:无特别说明,以下称呼的linux系统统一视为redhat系linux redhat系列linux系统 如果想修改主机名 很多人可能都会以为是: $hostname NEW-NAME 或者在 / ...
- 事件冒泡、事件委托、jQuery元素节点操作、滚轮事件与函数节流
一.事件冒泡定义 事件冒泡是指在一个对象触发某类事件(比如单击onclick事件),如果此对象定义了此事件的处理程序,那么此事件就会调用这个处理程序,如果没有定义此事件处理程序或者事件返回true,那 ...