[leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字。
public class Solution {
private static char[][] chars = {{'I', 'V'}, {'X', 'L'}, {'C', 'D'}, {'M', 'O'}};
private static HashMap<Character, Integer> map = null;
private static void init() {
map = new HashMap<Character, Integer>();
for (int i = 0; i < 4; i++) {
map.put(chars[i][0], (int) Math.pow(10, i));
map.put(chars[i][1], 5 * (int) Math.pow(10, i));
}
}
public static int romanToInt(String s) {
if (map == null) {
init();
}
int length = s.length();
int result = 0;
boolean flag = true;
int prev = map.get(s.charAt(0));
for (int i = 1; i < length; i++) {
int x = map.get(s.charAt(i));
if (prev >= x) {
result += prev;
} else {
result -= prev;
}
prev = x;
}
result += prev;
return result;
}
}
[leetcode] 13. Roman to Integer的更多相关文章
- 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 罗马数字转化成整数
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 - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- 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 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [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 ...
- [LeetCode] 13. Roman to Integer ☆☆
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
随机推荐
- 【Codeforces720D】Slalom 线段树 + 扫描线 (优化DP)
D. Slalom time limit per test:2 seconds memory limit per test:256 megabytes input:standard input out ...
- Java的发展历程
Java的发展历程充满了传奇色彩. 最初,Java是由Sun公司的一个研究小组开发出来的, 该小组起先的目标是想用软件实现对家用电器进行集成控制的小型控制装置. 开始,准备采用C++,但C++太复杂, ...
- 如何学习Oracle
如何学习Oracle?分清几个概念是关键 经常有一些Oracle的初学者问到以下几个问题,这里集中解答一下,希望对大家有帮助. 1.如果有一定的数据库基础,知道SQL是怎么回事,即使写不出 ...
- 在Activity之间传递参数(四)
获取Activity的返回参数(在参数(三)User的例子的基础上实现): 1.activity_the_aty.xml文件:<EditText android:id="@+id/ed ...
- 4、JavaScript
一.JavaScript的概念:是基于对象和事件的脚本语言. 1.特点: a).安全性. b).跨平台性(只要可以解释JS的浏览器就可以执行,和平台无关) 2.JavaScript与Java的区别: ...
- [Scala] akka actor编程(一)
Akka基础 Akka笔记之Actor简介 Akka中的Actor遵循Actor模型.你可以把Actor当作是人.这些人不会亲自去和别人交谈.他们只通过邮件来交流. 1. 消息传递 2. 并发 3 ...
- 如何撤销 PhpStorm/Clion 等 JetBrains 产品的 “Mark as Plain Text” 操作 ?
当把某个文件“Mark as Plain Text”时,该文件被当做普通文本,就不会有“代码自动完成提示”功能,如下图所示: 但是呢,右键菜单中貌似没有 相应的撤销 操作, 即使是把它删除,再新建一个 ...
- 【Go入门教程4】struct类型(struct的匿名字段)
struct Go语言中,也和C或者其他语言一样,我们可以声明新的类型,作为其它类型的属性或字段的容器.例如,我们可以创建一个自定义类型person代表一个人的实体.这个实体拥有属性:姓名和年龄.这样 ...
- input(file)样式修改及上传文件名显示
实现思路: a标签包裹input元素 设置a标签为上传按钮的样式,相对定位 设置input为透明,绝对定位,覆盖到a上面 效果:看到的按钮是a的样式,点击时实际是点击input元素.样式和功能都具备 ...
- Cordova phonegap开发环境搭建
1.下载并安装Android Studio, 2.下载并安装nodejs 3.通过nodejs来安装cordova(npm install -g cordova ) 4.使用cordova来创建pho ...