13. Roman to Integer (JAVA)
Roman numerals are represented by seven different symbols: I
, V
, X
, L
, C
, D
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 beforeV
(5) andX
(10) to make 4 and 9.X
can be placed beforeL
(50) andC
(100) to make 40 and 90.C
can be placed beforeD
(500) andM
(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)的更多相关文章
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- 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 ,即 ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- Leetcode 13. Roman to Integer(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- leetcode:Roman to Integer(罗马数字转化为罗马数字)
Question: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the rang ...
- 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 ...
- 【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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- c# 简单方便的连接oracle方式
通过nuget安装ManagedDataAccess (自动生成的config里面的配置都可以删掉) winform程序,拖出一个datagridview和button using Oracle.Ma ...
- 一则ORACLE进程都在但是无法进入实例的问题
[oracle@localhost ~]$ ps -ef|grep smonoracle 14809 1 0 Sep25 ? 00:13:02 ora_smon_mailp3[oracle@local ...
- webpy学(ban)习(砖)记录
参考链接:http://blog.csdn.net/caleng/article/details/5712850 参考代码:http://files.cnblogs.com/files/tacyeh/ ...
- 分布式 基本理论 CAP 之 各分布式系统的cap支持情况
分布式系统.理论.协议 非常非常多, 它们多cap 的支持是怎么样的呢? 需要注意的是,分布式系统 为了应付各种 复杂 应用场景,支持各种各样的功能,可能有的提供了选项或某种机制, 某个时刻,支持CP ...
- 原生js开发vue的双向数据绑定
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- eclipse启动tomcat访问http://localhost:8080 报404错误
eclipse正常启动tomcat,但是 访问http://localhost:8080 却报404错误 修改下配置 就好操作如下图 打开eclipse的server视图,双击配置好的那个tomcat ...
- Java 8 默认方法
转自:https://www.runoob.com/java/java8-default-methods.html Java 8 新增了接口的默认方法. 简单说,默认方法就是接口可以有实现方法,而且不 ...
- Java学习笔记 -- Java定时调度工具Timer类
1 关于 (时间宝贵的小姐姐请跳过) 本教程是基于Java定时任务调度工具详解之Timer篇的学习笔记. 什么是定时任务调度 基于给定的时间点,给定的时间间隔或者给定的执行次数自动执行的任务. 在Ja ...
- LevelDB源码分析-Write
Write LevelDB提供了write和put两个接口进行插入操作,但是put实际上是调用write实现的,所以我在这里只分析write函数: Status DBImpl::Write(const ...
- [phvia/dkc] Docker Compose 快速构建(LNMP+Node)运行环境
快速构建(LNMP+Node)运行环境. dkc 在此作为 docker-compose 的缩写,你可以理解为 alias dkc=docker-compose 准备 安装 docker 选择1) 从 ...