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. Spring事件通知机制

    在上图中,调用 getApplicationEventMulticaster()方法,该方法返回的ApplicationEventMulticaster类型的对象applicationEventMul ...

  2. ES6系列之let/const及块级作用域

    本系列是在平时阅读.学习.实际项目中有关于es6中的新特性.用发的简单总结,目的是记录以备日后温习:本系列预计包含let/const.箭头函数.解构.常用新增方法.Symbol.Set&Map ...

  3. 如何成功打造一款中台(PaaS)产品

    现如今,很多互联网公司在向“大中台,小前台”方向靠拢,通过打造高度可用.高度可定制的中台,来支撑前台业务的快速发展.个性化功能定制.但在构建中台产品(即所谓公司级平台)的时候,如何能成功让一款产品从0 ...

  4. dpdk环境配置

    1.配置流程 [1]修改/boot/grub2/grub.cfg,添加下列标记的配置项. linux16 /vmlinuz--.el7.x86_64 root=/dev/mapper/centos-r ...

  5. Python课程第五天作业

    1.利用字典推导式和列表推导式完成数据的相互转化: dic = {'name': 'Owen', 'age': 18, 'gender': '男'} ls = [('name', 'Owen'), ( ...

  6. iOS应用图标AppIcon

    应用图标需求:(像素px) 29pt:      58*58 ( @2x ) 87*87 ( @3x ) 40pt:      120*120 ( @2x ) 180*180 ( @3x ) 60pt ...

  7. 如何快速扫描C段(网站快照、后台识别/登录、目录扫描)

    1.C段扫描 C类地址范围从 192.0.0.1 到 223.255.255.254 ,192转换成二进制就是1100000:223转换成二进制就是1101111:所以说网络地址的最高位肯定是110开 ...

  8. update_db_inputs.conf

    #!/bin/bash#-------------------------------------------------------------------------------# Name: u ...

  9. Jmeter5.1.1创建一个http请求的压力测试

    1.首先添加一个线程组,在线程组中,配置压力情况 2.然后在线程组中,添加取样器,添加http请求:配置web服务器协议(http/https).服务器名称或IP.端口号.请求方法.路径等参数 3.然 ...

  10. gp工具的许可

    还是要在代码里许可 static class Program { [STAThread] static void Main() { ESRI.ArcGIS.RuntimeManager.Bind(ES ...