1. 原题链接

https://leetcode.com/problems/roman-to-integer/description/

2. 题目要求

(1)将罗马数字转换成整数;(2)范围1-3999;

3. 关于罗马数字

罗马数字相关规则已经在之前一篇博客里写过,这里不再赘述(之前博客的传送门

4. 解题思路

(1)这与之前第十二题Integer转换Roman虽然很相似,但处理方法并不相同。罗马数字更像是一个字符串,因此将其转换成字符数组进行处理。

(2)从后向前遍历一次,结合罗马数字的组合规则,根据该位置罗马数字之前位置累加起来的和进行大小比较,判断该位置是加还是减。

5. 代码实现

 public class RomantoInteger13 {
public static void main(String[] args) {
System.out.println(RomantoInteger13.romanToInt("MDCCCLXXXIV"));
}
public static int romanToInt(String s) {
int count = 0;
for(int i = s.length()-1;i>=0;i--){
char c = s.charAt(i);
switch (c){
case 'I':
count += (count>=5?-1:1);
break;
case 'V':
count +=5;
break;
case 'X':
count+=(count>=50?-10:10);
break;
case 'L':
count+=50;
break;
case 'C':
count+=(count>=500?-100:100);
break;
case 'D':
count+=500;
break;
case 'M':
count+=1000;
break;
} } return count; }
}

LeetCode:12. Roman to Integer (Easy)的更多相关文章

  1. 【leetcode】Integer to Roman & Roman to Integer(easy)

    Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...

  2. LeetCode:14. Longest Commen Prefix(Easy)

    1. 原题链接 https://leetcode.com/problems/longest-common-prefix/description/ 2. 题目要求 给定一个字符串数组,让你求出该数组中所 ...

  3. 「Leetcode」13. Roman to Integer(Java)

    分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...

  4. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  5. Java-Runoob-高级教程-实例-方法:12. Java 实例 – Enum(枚举)构造函数及方法的使用-um

    ylbtech-Java-Runoob-高级教程-实例-方法:12. Java 实例 – Enum(枚举)构造函数及方法的使用 1.返回顶部 1. Java 实例 - Enum(枚举)构造函数及方法的 ...

  6. LeetCode:40. Combination Sum II(Medium)

    1. 原题链接 https://leetcode.com/problems/combination-sum-ii/description/ 2. 题目要求 给定一个整型数组candidates[ ]和 ...

  7. Windows Phone本地数据库(SQLCE):12、插入数据(翻译)

    这是“windows phone mango本地数据库(sqlce)”系列短片文章的第十二篇. 为了让你开始在Windows Phone Mango中使用数据库,这一系列短片文章将覆盖所有你需要知道的 ...

  8. LeetCode:7. Reverse Integer(Easy)

    题目要求:将给出的整数进行逆序输出 注意:整数的最大范围-2147483648-2147483647,当翻转后的数超出范围后返回0 思路:对给出的整数除以10,取余和取整:然后对取整部分继续取余和取整 ...

  9. LeetCode:5. Longest Palindromic Substring(Medium)

    原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/ 1. 题目要求:找出字符串中的最大回文子串 ...

随机推荐

  1. 二维数组展示到DataGridView(c#)

    窗体程序中二维数组展示到DataGridView public void TwoDArrayShowINDatagridview(string[,] arr) { DataTable dt = new ...

  2. POJ 2985 名次树

    题意:1~n个猫,有合并操作,有询问操作,合并两只猫所在的集合,询问第K大的集合. 分析:合并操作用并查集,用size维护,询问操作用Treap.注意优化,不能刚开始就把所有size = 1放到名次树 ...

  3. Codeforces Round #422 (Div. 2)

    Codeforces Round #422 (Div. 2) Table of Contents Codeforces Round #422 (Div. 2)Problem A. I'm bored ...

  4. Uva 4394 字符串刷子

    题目链接:https://vjudge.net/contest/164840#problem/A 题意:一个字符串刷子,每次可以将一段连续的字符串变成一种颜色,给两个字符串,最少通过几次可以将第一个字 ...

  5. 【[SCOI2007]修车】

    题目 只能做网络流度日了 当然是要对每个修车的人拆点,把每个人拆成\(n\)个点用于接收不同时刻的车 每个车\(i\)向每个时刻\(k\)的人\(j\)连边,边权为\(t[i][j]*k\)这样就是这 ...

  6. (第六场)Singing Contest 【模拟】

    题目链接:https://www.nowcoder.com/acm/contest/144/A 标题:A.Singing Contest | 时间限制:1 秒 | 内存限制:256M Jigglypu ...

  7. msfconsole_无法启动问题

    service postgresql start # 启动数据库服务 msfdb init # 初始化数据库 msfconsole # 启动metasploit

  8. html上传图片(进度条变化)、音乐

    <html> <head> <title>$Title$</title> </head> <link href="css/b ...

  9. java GZIP 压缩数据

    package com.cjonline.foundation.cpe.action; import java.io.ByteArrayInputStream; import java.io.Byte ...

  10. 初试Taro

    今天有空在github上发现一个好东西--Taro. 京东的Team打造的多端开发解决方案.·一套代码编译成可以在多端运行代码,(小程序,RN,H5)看到这里我就不淡定了. 这个意思就是,你照常写你的 ...