Roman To Integer leetcode java
问题描述:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
算法:
/**
* 输入一个罗马数字,返回它的整型表示
* @author admin
* 转换规则:从右向左或者从左向右遍历均可
* 1 相同数字连写,相加 ;2 小数字在大数字右边,相加;3 小数字在大数字左边,大的减去小的
*/
方法一:hashmap 从右向左遍历
//将罗马数字转换为整型值
public static int romanToInt(String s){
HashMap<Character, Integer> roma_weight = new HashMap<Character, Integer>();
roma_weight.put('I', 1);
roma_weight.put('V', 5);
roma_weight.put('X',10);
roma_weight.put('L', 50);
roma_weight.put('C', 100);
roma_weight.put('D',500);
roma_weight.put('M',1000);
char[] roma = s.toCharArray(); //将罗马数字转化为字符数组
int length = roma.length;
int preValue = 0; //前一步的value
int value = roma_weight.get(roma[length - 1]) ;// 当前的value //从右向左遍历
for(int i = length - 2; i >= 0 ; i--){
//小的数字在大数字右边,直接相加
if( roma_weight.get(roma[i]) >= roma_weight.get(roma[i + 1])) {
preValue = value;
value = value + roma_weight.get(roma[i]);
}
else { //小的数字在大数字左边,大的减去小的
value = preValue + roma_weight.get(roma[i + 1]) - roma_weight.get(roma[i]);
}
}
return value;
}
方法二:switch case,从左向右遍历
//与第一种方法基本相同,从左向右
public static int romanToInt1(String s) {
char[] ss = s.toCharArray();
int ret = toNumber(ss[0]);
for (int i = 1; i < ss.length; i++) {
if (toNumber(ss[i - 1]) < toNumber(ss[i])) {
//ret - toNumber(ss[i - 1]) = preValue; 然后再进行 preValue + toNumber(ss[i]) - toNumber(ss[i - 1]);
ret += toNumber(ss[i]) - 2 * toNumber(ss[i - 1]); //相当于方法一中的 preValue + roma_weight.get(roma[i + 1]) - roma_weight.get(roma[i]);
} else {
ret += toNumber(ss[i]);
}
}
return ret;
} //罗马数字权重表,没有空间复杂度,较hashmap要好一些
public static int toNumber(char ch) {
switch (ch) {
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;
}
Roman To Integer leetcode java的更多相关文章
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- Roman to Integer -- LeetCode 13
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- Roman to Integer [LeetCode]
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...
- 13. Roman to Integer (JAVA)
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- Reverse Integer LeetCode Java
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public cl ...
- LeetCode 13. 罗马数字转整数(Roman to Integer)
13. 罗马数字转整数 13. Roman to Integer 题目描述 罗马数字包含以下七种字符: I,V,X,L,C,D 和 M. 字符 数值 I 1 V ...
- [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 解题报告
Roman to IntegerGiven a roman numeral, convert it to an integer. Input is guaranteed to be within th ...
随机推荐
- TI 多模雷达1843毫米波雷达做自动泊车(用了8个雷达)
http://e2e.ti.com/blogs_/b/behind_the_wheel/archive/2019/01/09/how-mmwave-sensors-enable-autonomous- ...
- hihoCoder week15 最近公共祖先·二
tarjan求lca 就是dfs序中用并查集维护下,当访问到询问的第二个点u的时候 lca就是第一点的find(fa[v]) fa[v] = u; // 当v为u的儿子 且 v已经dfs完毕 #i ...
- python 之 循环语句
python提供了for循环和while循环以及嵌套循环(在python中没有do..while循环) while 循环语法: while 判断条件: 执行语句...... 实际案例: numbers ...
- 论文笔记:A Review on Deep Learning Techniques Applied to Semantic Segmentation
A Review on Deep Learning Techniques Applied to Semantic Segmentation 2018-02-22 10:38:12 1. Intr ...
- js运算符的一些特殊应用
作者: 小文 来源: http://www.cnblogs.com/daysme/ 时间: 2017/3/2 17:21:03 本文集合了了js运算符的一些特殊应用. js位运行符的运用. js运算符 ...
- 在服务器端对sshd做白名单
1.添加用户 #useradd aaa #passwd aaa -->输入密码:123456 添加3个用户,bbb和ccc与aaa添加一样 2.添加白名单 #vim /etc/ssd/sshd_ ...
- python实现八皇后问题
import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...
- linux 进阶命令笔记(12月26日)
1. df 指令 作用:查看磁盘空间 用法: #df -h -h 表示以可读性较高的形式展示大小 2.free 指令 作用:查看内存使用情况 语法:#free -m -m表 ...
- 单域名下多子域名同时认证HTTPS
参考: http://blog.csdn.net/wzj0808/article/details/53401101 http://www.cnblogs.com/silin6/p/5931640.ht ...
- react native 第三方组件react-native-swiper 轮播组件
github地址:https://github.com/leecade/react-native-swiper 使用方法:安装:npm i react-native-swiper –save 查看模块 ...