Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
class Solution {
public int romanToInt(String s) {
String sym = "MDCLXVI";
int[] val = {1000,500,100,50,10,5,1};
int num = 0;
int p; //array pointer
for(int i = 0; i < s.length(); i++){
p = sym.indexOf(s.charAt(i));
if(i+1 < s.length() && sym.indexOf(s.charAt(i+1)) < p){
num = num + val[sym.indexOf(s.charAt(i+1))] - val[p];
i++;
}
else{
num += val[p];
}
}
return num;
}
}

解题思路:要处理的一个特殊情况是4,9,40...这类数,所以我们要扫描到当前位之后的那一位,判断是不是这种情况,做相应处理。

13. Roman to Integer (JAVA)的更多相关文章

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

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

  2. 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 ,即 ...

  3. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  4. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

  5. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  6. leetcode:Roman to Integer(罗马数字转化为罗马数字)

    Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...

  7. C# 写 LeetCode easy #13 Roman to Integer

    13.Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and  ...

  8. 【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 ...

  9. 13. Roman to Integer【leetcode】

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

随机推荐

  1. SfMLearner 记录

    2019年3月2日09:29:54 正在看SfMLearner的pytorch源码,意识到无监督的深度估计最重要的是利用实体的一致性 来建立loss. 对于一个不移动的物体,相机从一个pose到另一个 ...

  2. redis 中的key值过期后,触发通知事件

    1.创建springboot工程,创建监听类 maven配置 <dependencies> <dependency> <groupId>org.springfram ...

  3. 使用iptables基于MAC地址进行访控

    近日完成一台基于CentOS的SVN服务器配置,由于该服务器上的文件非常重要,仅部分用户需要访问,最后决定采用iptables来做访控,并且是根据MAC地址来限制,为了便于后期维护,防火墙的配置是通过 ...

  4. ocr_vote disk损坏恢复

    1. 检查votedisk和 ocr备份[root@rh6rac1 bin]./ocrconfig -showbackuprh6rac1     2019/03/19 16:57:40     /or ...

  5. ftp服务器使用-windowsftp服务起搭建

    首先打开控制面板选择程序 点击启动或关闭windows功能 勾选ftp服务器和IIS管理控制台点击确定 然后右键点击我的电脑,点击管理,然后点击本地用户和组,然后右键点击用户,点击新用户,创建一个用户 ...

  6. 前三次OO作业小结

    I used to be enamored of object-oriented programming. I'm now finding myself leaning toward believin ...

  7. 用 Python 获取 B 站播放历史记录

    用 Python 获取 B 站播放历史记录 最近 B 站出了一个年度报告,统计用户一年当中在 B 站上观看视频的总时长和总个数.过去一年我居然在 B 站上看了2600+个视频,总计251个小时,居然花 ...

  8. 团队项目需求分析——NABCD

    N(Need)需求 经发现,很多人在理发时都要经过漫长的排队等待时间,这些时间也就因此白白浪费掉了,而且一些理发店也会因个别顾客不愿等待而损失客源.对此,我们设计出了这款小软件——理了么,一款专门为理 ...

  9. Vim常用配置

    mkdir -p ~/.vim/bundle/Vundle.vim git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bundle/V ...

  10. css边框的一些属性

    边框样式值如下:none : 无边框.与任何指定的border-width值无关hidden : 隐藏边框.IE不支持dotted : 在MAC平台上IE4+与WINDOWS和UNIX平台上IE5.5 ...