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.
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 ...
随机推荐
- codeforces 1156E Special Segments of Permutation
题目链接:https://codeforc.es/contest/1156/problem/E 题目大意: 在数组p中可以找到多少个不同的l,r满足. 思路: ST表+并查集. ST表还是需要的,因为 ...
- 推荐Calendar操作日期
package com.example.demo.Calender; import java.text.SimpleDateFormat;import java.util.Calendar;impor ...
- [Python3] 002 Python3 中常用的命名规则
目录 1. 什么可以用来命名? 1.1 老三样: 字母.数字.下划线 1.2 其他 2. 什么不能用来命名? Python3 中的"关键字" 3. 命名"小贴士" ...
- 【转】MySQL查询缓存详解
[转]MySQL查询缓存详解 转自:https://www.cnblogs.com/Alight/p/3981999.html 相关文章:http://www.zsythink.net/archive ...
- select count(*) 底层到底干了啥?
作者:贾春生,http://dwz.win/myg SELECT COUNT( * ) FROM TABLE 是个再常见不过的 SQL 需求了. 在 MySQL 的使用规范中,我们一般使用事务引擎 I ...
- tensorflow学习笔记二----------变量
tensorflow里面的变量表示,需要使用特定的语法进行.如果想构造一个行(列)向量,需要调用Variable函数进行.对两个变量进行操作,也要调用相应的函数. import tensorflow ...
- D-多连块拼图
多连块是指由多个等大正方形边与边连接而成的平面连通图形. – 维基百科 给一个大多连块和小多连块,你的任务是判断大多连块是否可以由两个这样的小多连块拼成.小多连块只能平移,不能旋转或者翻转.两个小多连 ...
- ps的一点快捷键
选区工具快捷键(shift alt很重要) 按M键切换到选区工具 矩形选区-> shift 正方形 shift+m 矩形/椭圆来回切换 参考线:alt+v 选中上方的工具 alt+v+e 新建参 ...
- 剑指offer-动态规划-贪心算法--剪绳子-python
题目描述 给你一根长度为n的绳子,请把绳子剪成m段(m.n都是整数,n>1并且m>1),每段绳子的长度记为k[0],k[1],...,k[m].请问k[0]xk[1]x...xk[m]可能 ...
- json与导入模块目录
import json """主要用于不同语言的数据公用 """ info = {"a":1,"b" ...