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

  1. Symbol Value
  2. I 1
  3. V 5
  4. X 10
  5. L 50
  6. C 100
  7. D 500
  8. 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:

  1. Input: "III"
  2. Output: 3

Example 2:

  1. Input: "IV"
  2. Output: 4

Example 3:

  1. Input: "IX"
  2. Output: 9

Example 4:

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

Example 5:

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

解题思路:要处理的一个特殊情况是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. c# 简单方便的连接oracle方式

    通过nuget安装ManagedDataAccess (自动生成的config里面的配置都可以删掉) winform程序,拖出一个datagridview和button using Oracle.Ma ...

  2. 一则ORACLE进程都在但是无法进入实例的问题

    [oracle@localhost ~]$ ps -ef|grep smonoracle 14809 1 0 Sep25 ? 00:13:02 ora_smon_mailp3[oracle@local ...

  3. webpy学(ban)习(砖)记录

    参考链接:http://blog.csdn.net/caleng/article/details/5712850 参考代码:http://files.cnblogs.com/files/tacyeh/ ...

  4. 分布式 基本理论 CAP 之 各分布式系统的cap支持情况

    分布式系统.理论.协议 非常非常多, 它们多cap 的支持是怎么样的呢? 需要注意的是,分布式系统 为了应付各种 复杂 应用场景,支持各种各样的功能,可能有的提供了选项或某种机制, 某个时刻,支持CP ...

  5. 原生js开发vue的双向数据绑定

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. eclipse启动tomcat访问http://localhost:8080 报404错误

    eclipse正常启动tomcat,但是 访问http://localhost:8080 却报404错误 修改下配置 就好操作如下图 打开eclipse的server视图,双击配置好的那个tomcat ...

  7. Java 8 默认方法

    转自:https://www.runoob.com/java/java8-default-methods.html Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不 ...

  8. Java学习笔记 -- Java定时调度工具Timer类

    1 关于 (时间宝贵的小姐姐请跳过) 本教程是基于Java定时任务调度工具详解之Timer篇的学习笔记. 什么是定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Ja ...

  9. LevelDB源码分析-Write

    Write LevelDB提供了write和put两个接口进行插入操作,但是put实际上是调用write实现的,所以我在这里只分析write函数: Status DBImpl::Write(const ...

  10. [phvia/dkc] Docker Compose 快速构建(LNMP+Node)运行环境

    快速构建(LNMP+Node)运行环境. dkc 在此作为 docker-compose 的缩写,你可以理解为 alias dkc=docker-compose 准备 安装 docker 选择1) 从 ...