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:
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) {
int ans = ;
int top = ;
int len = s.length();
while(top<len){
if(s[top]=='M'){
ans += ;
top++;
}
else if(s[top]=='D'){
ans += ;
top++;
}
else if(s[top]=='C'){
if(s[top+]=='D'){
ans += ;
top+=;
}
else if(s[top+]=='M'){
ans +=;
top+=;
}
else {
ans += ;
top++;
}
}
else if(s[top]=='L'){
ans+=;
top++;
}
else if(s[top]=='X'){
if(s[top+]=='C'){
ans += ;
top+=;
}
else if(s[top+]=='L'){
ans += ;
top+=;
}
else {
ans += ;
top++;
}
}
else if(s[top]=='V'){
ans+=;
top++;
}
else if(s[top]=='I'){
if(s[top+]=='X'){
ans += ;
top+=;
}
else if(s[top+]=='V'){
ans += ;
top+=;
}
else {
ans += ;
top++;
}
}
}
return ans;
}
};
Leetcode 13. Roman to Integer(水)的更多相关文章
- 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 罗马数字转化成整数
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 - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- Java [leetcode 13] Roman to Integer
问题描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range fr ...
- 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 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- [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 1 t ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- 深入分析微博密码加密and百万级数据采集分享(登录篇)
一.前言 此文章主要是对微博微博关键词的检索结果进行采集,但是微博的采集需要登陆,所以此程序分为登陆程序和爬虫程序两部分: 微博要实现规模性数据采集自然少不了大量账号,这就需购买大量账号以及批量登陆, ...
- JAVA基础面向对象分析
面向对象内存的分析: 一:内存的分类 1:栈(tack) 2:堆(heop) 3: 静态区 4:代码区 二:引用数据类型内存特点 三:引用数据类型传值的特点 四:引用数据类型在作为参数时的特点 面向对 ...
- Luogu P1315 [NOIP2012]观光公交
题目 每次把加速器用在可以是答案减少最多的地方就即可.(这不是废话吗?) 具体而言,我们处理出: \(sum_i\)到\(i\)为止下车人数之和. \(t_i\)在\(i\)最晚的上车的人的上车时间. ...
- tarjan算法应用 割点 桥 双连通分量
tarjan算法的应用. 还需多练习--.遇上题目还是容易傻住 对于tarjan算法中使用到的Dfn和Low数组. low[u]:=min(low[u],dfn[v])--(u,v)为后向边,v不是u ...
- Node+Express+MySql实现简单增删改查和登录
var express = require('express'); var mysql = require('mysql'); var app = express(); var bodyParser ...
- Node.js+webSocket
// 引入WebSocket模块 var ws = require('nodejs-websocket') var PORT = 3030 var server = ws.createServer(f ...
- 在Myeclipse下查看Java字节码指令信息
在实际项目开发中,有时为了了解Java编译器内部的一些工作,需要查看Java文件对应的具体的字节码指令集,这里提供两种方式供参考. 一.使用javap命令 javap是JDK提供的 ...
- php手动实现ip2long和long2ip
php手动实现ip2long和long2ip /** * 测试 */ public function testipAction() { $ip = '10.58.101.175'; echo ip2l ...
- empty()、isset()、is_null()的区别
总结:1. 变量有二种状态: 已声明, 未声明2. 已声明的变量也有二种状态: 已赋值(初始化), 未赋值(未初始化)3. 变量可能会被赋值类型: null, 空值, 非空值 3.1: null值: ...
- 当页面完全加载完成后执行一个JS函数
方法1.如下程序,当页面完全加载后执行openTheIndexPage()方法 <html> <head> <meta http-equiv="Conte ...