算法练习题---罗马数字转int
连接:https://leetcode-cn.com/problems/roman-to-integer/submissions/
题目:
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
字符 数值
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
示例 1:
输入: "III"
输出: 3
示例 2:
输入: "IV"
输出: 4
示例 3:
输入: "IX"
输出: 9
示例 4:
输入: "LVIII"
输出: 58
解释: L = 50, V= 5, III = 3.
示例 5:
输入: "MCMXCIV"
输出: 1994
解释: M = 1000, CM = 900, XC = 90, IV = 4.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/roman-to-integer
其中,方法二、三为博主本人解答。方法一,目前为查找的力扣中运行耗时相对较短的解法
解法一:
package com.zx.leetcode.roman2integer; /**
* @Author JAY
* @Date 2019/6/16 14:25
* @Description 罗马数字转int
**/
public class SolutionV3 { public static void main(String[] args) {
System.out.println(romanToInt("LVIII"));
} public static int romanToInt(String s) {
char[] romanArray = s.toCharArray();
int result = 0;
int index = 0;
while (index < romanArray.length) { switch (romanArray[index++]) {
case 'I':
if (index < romanArray.length && romanArray[index] == 'V') {
result += 4;
index++;
} else if (index < romanArray.length && romanArray[index] == 'X') {
result += 9;
index++;
} else {
result += 1;
}
break;
case 'V':
result += 5;
break;
case 'X':
if (index < romanArray.length && romanArray[index] == 'L') {
result += 40;
index++;
} else if (index < romanArray.length && romanArray[index] == 'C') {
result += 90;
index++;
} else {
result += 10;
}
break;
case 'L':
result += 50;
break;
case 'C':
if (index < romanArray.length && romanArray[index] == 'D') {
result += 400;
index++;
} else if (index < romanArray.length && romanArray[index] == 'M') {
result += 900;
index++;
} else {
result += 100;
}
break;
case 'D':
result += 500;
break;
case 'M':
result += 1000;
break;
default:
throw new IllegalArgumentException("The input isn't roman.");
}
}
return result;
}
}
解法二:
package com.zx.leetcode.roman2integer; import org.springframework.util.StringUtils; import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap; /**
* @Author JAY
* @Date 2019/6/16 14:25
* @Description 罗马数字转int
**/
public class SolutionV2 { public static void main(String[] args) {
System.out.println(romanToInt("LVIII"));
} public static int romanToInt(String s) {
if (null == s){
return 0;
}
int sum = 0; TreeMap<String, Integer> romanNumAll = new TreeMap<String, Integer>();
romanNumAll.put("I",1);
romanNumAll.put("V",5);
romanNumAll.put("X",10);
romanNumAll.put("L",50);
romanNumAll.put("C",100);
romanNumAll.put("D",500);
romanNumAll.put("M",1000); Map<String, Integer> romanNumSort = new HashMap<>(7);
romanNumSort.put("I",1);
romanNumSort.put("V",2);
romanNumSort.put("X",3);
romanNumSort.put("L",4);
romanNumSort.put("C",5);
romanNumSort.put("D",6);
romanNumSort.put("M",7); char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++){
String charI = String.valueOf(chars[i]);
if (i + 1 >= chars.length){
sum = sum + romanNumAll.get(charI);
break;
}
String charI1 = String.valueOf(chars[i + 1]);
if (romanNumSort.get(charI) >= romanNumSort.get(charI1)){
sum = sum + romanNumAll.get(charI);
} else {
sum = sum + (romanNumAll.get(charI1) - romanNumAll.get(charI));
i = i + 1;
}
}
if (sum > 3999){
return 3999;
}
return sum;
}
}
解法三:
package com.zx.leetcode.roman2integer; import org.springframework.util.StringUtils; import java.util.HashMap;
import java.util.Map; /**
* @Author JAY
* @Date 2019/6/16 14:25
* @Description 罗马数字转int
**/
public class Solution { public static void main(String[] args) {
System.out.println(romanToInt("IX"));
} public static int romanToInt(String s) {
if (s == null){
return 0;
}
Map<String, Integer> romanNumAll = new HashMap<>(7);
romanNumAll.put("I",1);
romanNumAll.put("V",5);
romanNumAll.put("X",10);
romanNumAll.put("L",50);
romanNumAll.put("C",100);
romanNumAll.put("D",500);
romanNumAll.put("M",1000); Map<String, Integer> romanNum = new HashMap<>(6);
romanNum.put("IV",4);
romanNum.put("IX",9);
romanNum.put("XL",40);
romanNum.put("XC",90);
romanNum.put("CD",400);
romanNum.put("CM",900);
int sum = 0; for (Map.Entry<String , Integer> entry : romanNum.entrySet()){
if (s.contains(entry.getKey())){
sum = sum + entry.getValue();
s = s.replace(entry.getKey(),"_");
}
}
String[] split = s.split("_");
if (split != null && split.length > 0){
for (String s1 : split) {
char[] chars = s1.toCharArray();
for (char aChar : chars) {
sum = sum + romanNumAll.get(String.valueOf(aChar));
}
}
}
if (sum > 3999){
return 3999;
}
return sum;
}
}
算法练习题---罗马数字转int的更多相关文章
- 13 Roman to Integer(罗马数字转int Easy)
题目意思:罗马数字转int 思路:字符串从最后一位开始读,IV:+5-1 class Solution { public: int romanToInt(string s) { map<char ...
- 【算法】 string 转 int
[算法] string 转 int 遇到的一道面试题, 当时只写了个思路, 现给出具体实现 ,算是一种比较笨的实现方式 public class StringToInt { /// <summa ...
- leetcode算法13.罗马数字转整数
哈喽!大家好,我是[学无止境小奇],一位热爱分享各种技术的博主! [学无止境小奇]的创作宗旨:每一条命令都亲自执行过,每一行代码都实际运行过,每一种方法都真实实践过,每一篇文章都良心制作过. [学无止 ...
- Java算法练习——罗马数字转整数
题目链接 题目描述 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 ...
- 罗马数字转int
// I(1).V(5).X(10).L(50).C(100).D(500)和M(1000) 1.重复数次:一个罗马数字重复几次,就表示这个数的几倍.2.右加左减:2.1 在较大的罗马数字的右边记上较 ...
- P2093 零件分组【贪心算法练习题】
题目链接: http://codevs.cn/problem/4888/ https://www.luogu.org/problem/show?pid=2093 题目描述 某工厂生产一批棍状零件,每个 ...
- bzoj4534: 基础排序算法练习题
传送门 策爷的论文题啊……题解在这儿 我只想知道为什么这题的弱化版会出现在我们今天的%你赛里…… 题意:给你一堆操作$(l,r)$,表示将区间$(l,r)$按升序排序.以及$q$个询问,每次询 ...
- PHP算法之罗马数字转整数
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 ...
- 算法-java代码实现基数排序
基数排序 第11节 基数排序练习题 对于一个int数组,请编写一个基数排序算法,对数组元素排序. 给定一个int数组A及数组的大小n,请返回排序后的数组.保证元素均小于等于2000. 测试样例: [1 ...
随机推荐
- Kubernetes第十一章--部署微服务电商平台
- vue学习(1) vue-cli 项目搭建
vue学习(1) vue-cli 项目搭建 一.windows环境 1. 下载node.js安装包 官网:https://nodejs.org/en/download/ 选择LTS下载 2. 安装 ...
- js删除对象里的某一个属性
var a={"id":1,"name":"danlis"}; //添加属性 a.age=18; console.log(a); //结果: ...
- webpack练手项目之easySlide(二):代码分割
Hello,大家好. 在上一篇 webpack练手项目之easySlide(一):初探webpack 中我们一起为大家介绍了webpack的基本用法,使用webpack对前端代码进行模块化打包. 但 ...
- OSX - Mac OS 10.12后Caps lock(大写键)无法使用的解决办法
我在OSX的虚拟机中安装了windows 7 操作系统,但是发现在win7下,大写键不起作用,通过下面方面搞定了! ▲打开设置中的键盘选项,并切换至输入源选项标签, ▲取消勾选“使用大写锁定键来回切换 ...
- unity 实现技能释放
要实现技能释放其实很简单,说白了就是在指定的位置Instantiate一个对应的例子特效.我走的弯路主要在寻找这个指定位置上. 对于指向性技能就不多说了,因为是有确切目标的(当然首先判断下技能能不能对 ...
- list集合排序案例
// List集合排序: Collections.sort(list, new Comparator<Object>(){ public int compare(Object obja, ...
- Linux kernel启动选项(参数)(转)
Linux kernel启动选项(参数) 转载链接https://www.cnblogs.com/linuxbo/p/4286227.html 在Linux中,给kernel传递参数以控制其行为总共 ...
- jenkins使用邮件功能
jenkins发送邮件 在日常构建后,需要及时将构建结果发送给相应的人员.这时就可以使用jenkins自带的邮件配置系统. 1 开通邮箱的SMTP服务,需要发送短信验证开启 2 进入"系统管 ...
- 【转】Go调度器原理浅析
goroutine是golang的一大特色,或者可以说是最大的特色吧(据我了解),这篇文章主要翻译自Morsing的[这篇博客](http://morsmachine.dk/go-scheduler) ...