[leetcode]13. Roman to Integer罗马数字转整数
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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan 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.
Solution1: Simulation
For most cases, Roman Numeral use addition(相加), like 6 -> "V I" (which means previous Roman > current Roman)
result += map(s.charAt(i));
But a few cases uses subtraction(相减) , like 4->"I V" ( which means previous Roman < current Roman)
result += (map(s.charAt(i)) - 2 * map(s.charAt(i - 1)));
code:
/*
Time Complexity: O(n)
Space Complexity: O(1)
*/ class Solution {
public int romanToInt(String s) {
int result = 0;
for (int i = 0; i < s.length(); i++) {
if (i > 0 && map(s.charAt(i)) > map(s.charAt(i - 1))) {
result += (map(s.charAt(i)) - 2 * map(s.charAt(i - 1)));
} else {// else包含了 i = 0 || map(s.charAt(i))<= map(s.charAt(i-1)))
result += map(s.charAt(i));
}
}
return result;
}
private static int map(char c) {
switch (c) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
default: return 0;
}
}
}
[leetcode]13. Roman to Integer罗马数字转整数的更多相关文章
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- [LeetCode] 13. Roman to Integer 罗马数字转化成整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【LeetCode】Roman to Integer(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- 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(水)
13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- LeetCode 13 Roman to Integer 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- leetcode 13 Roman to Integer 罗马数组转整型
描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...
- LeetCode - 13. Roman to Integer - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
随机推荐
- IETF和W3C的区别
国际互联网协会ISOC和万维网联盟W3C是互联网领域内两大国际协会组织,ISOC旗下IETF机构与W3C是互联网行业内两大标准组织.不可否认「IETF和W3C都是优秀的组织,它们从事着一种使事情按程序 ...
- Java面向对象 第5节 抽象类和接口
一.抽象类和抽象方法 区分抽象方法和普通方法1)当一个方法被abstract修饰时,该方法成为抽象方法2)抽象类所在的类必须定义为抽象类3)抽象方法不会有具体的实现,而是在抽象类的子类中通过方法重写进 ...
- webpack 中,importloaders 配置项的含义
importLoaders:用于配置「css-loader 作用于 @import 的资源之前」有多少个 loader. 0 => no loaders (default); 1 => p ...
- 基于redis的 分布式锁 Java实现
package com.hs.services.lock; import java.util.concurrent.TimeUnit; import javax.annotation.Resource ...
- [C#]如何将 string 安全地转换为 int
当遇到string向int类型转换时会遇到以下问题: 1.字符串非数字格式 2.字符串描述的不是int型 3.转换后越界 这些情况都需要用try catch来捕获异常并处理 安全简单的转换可以用 In ...
- Xtrabackup的安装与使用
Xtrabackup的安装与使用 1. XtraBackup 简介 XtraBackup(PXB) 工具是 Percona 公司用 perl 语言开发的一个用于 MySQL 数据库物理热备的备份工具, ...
- Java环境配置之JDK安装
一.下载 现在JDK的版本很多.我下载的是jdk1.7 以下链接是jdk1.8的 http://www.oracle.com/technetwork/java/javase/downloads/jdk ...
- 设置Tomcat管理员用户名和密码
http://dove19900520.iteye.com/blog/1774980 今天tomcat出点问题,然后我就想进入tomcat manager看看,结果怎么输入密码都不行,后来网上查了查才 ...
- 3.CM3内核架构-寄存器
一.寄存器的种类
- 读书笔记——《redis入门指南(第2版)》第三章 入门
3 .redis的5种数据类型及相应命令 redis不区分命令大小写. string 512m 一个散列类型键可包含至多232-1个字段 一个列表类型键最多能容纳232-1个元素 一个集合类型键最多能 ...