LeetCode记录之13——Roman to Integer
能力有限,这道题采用的就是暴力方法,也只超过了39%的用户。需要注意的就是罗马数字如果IXC的后一位比前一位大的采取的是减的方式。
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
给定一个罗马数字,将其转换为整数。
输入保证在1到3999之间。
class Solution {
public int romanToInt(String s) {
int length=s.length();
int num=0;
for(int i=0;i<length;i++){
switch (s.charAt(i)) {
case 'I':{
if((i+1!=length)&&s.charAt(i+1)!='I'){
num-=1;
break;
}
else {
num+=1;
break;
}
}
case 'X':
if((i+1!=length)&&((s.charAt(i+1)=='M')||(s.charAt(i+1)=='D')||(s.charAt(i+1)=='C')||(s.charAt(i+1)=='L'))){
num-=10;
break;
}
else {
num+=10;
break;
}
case 'C':
if((i+1!=length)&&((s.charAt(i+1)=='M')||(s.charAt(i+1)=='D'))){
num-=100;
break;
}
else {
num+=100;
break;
}
case 'M':
num+=1000;
break;
case 'V':
num+=5;
break;
case 'L':
num+=50;
break;
case 'D':
num+=500;
break;
default:
break;
}
}
return num;
}
}
LeetCode记录之13——Roman to Integer的更多相关文章
- 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&Python] Problem 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❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- 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 (2 solutions)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 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 ...
- 13. Roman to Integer【leetcode】
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- iis8不支持 aspnet_regiis.exe -iru 命令的解决办法
服务器版的限制,我看你给的提示说也可以使用 dism.exe 命令行. C:\> DISM /Online /Enable-Feature /FeatureName:WCF-HTTP-Activ ...
- C# ShowDialog时窗体贱传值得方法
用C#开发应用的时候,通常需要多个窗体!有时候为了打开窗体的时候禁止操作父窗体,我们一般采用模态对话框的方法,也算就是用ShowDialog()方法. 假设有两个窗体A和B,A作为主窗体使用ShowD ...
- 数字图像处理实验(16):PROJECT 06-03,Color Image Enhancement by Histogram Processing 标签: 图像处理MATLAB 2017
实验要求: Objective: To know how to implement image enhancement for color images by histogram processing ...
- Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows
01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...
- 丢弃昂贵的Detours Professional 3.0,使用免费强大的EasyHook
我们要先看看微软官方的著名HOOK库: Detours Professional 3.0 售价:US$9,999.95 功能列表: Detours 3.0 includes the following ...
- javascript总结13:循环语句
1 While循环 While(条件表达式){ 只要条件表达式结果为true,循环一直执行,当条件表达式结果为false的时候,循环终止 } While循环语句需现在循环体外定义变量. 2 for循环 ...
- Schwartz kernel theorem施瓦兹核定理
In mathematics, the Schwartz kernel theorem is a foundational result in the theory of generalized fu ...
- vs2012 许可 tfs 许可
Team Foundation Server 2012序列号或MSDN版本 BVGTF-T7MVR-TP46H-9Q97G-XBXRB VS2012注册码 亲测成功.我的是旗舰版... YKCW6-B ...
- linux 查看 PHP 的默认版本。
命令 env env:显示当前用户的环境变量: which php
- 【大数据之数据仓库】GreenPlum PK DeepGreen(TPCH)
1.背景 一张UML类图可以简单的说明GreenPlum和DeepGreen之间的关系: GreenPlum: 主页:http://greenplum.org/ 源码:开源,https://githu ...