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 ...
随机推荐
- 带你剖析WebGis的世界奥秘----瓦片式加载地图(转)
带你剖析WebGis的世界奥秘----瓦片式加载地图 转:https://zxhtom.oschina.io/zxh/20160805.html 编程 java 2016/08/05 0留言, ...
- centos 搭建docker环境
我有一台便宜的腾讯云服务器,当然配置自然也是最低的,只是用来平常玩一玩,学习的用处,下面介绍一下我在上面搭建docker的心得,共勉一下. 安装与配置 Docker 安装 Docker Docker ...
- 面试题:try,catch,finally都有return语句时执行哪个 已看1
1.不管有木有出现异常,finally块中代码都会执行: return 先执行 把值临时存储起来, 执行完finally之后再取出来 值是不会改变的2.当try和catch中有return时,fina ...
- $.post()参数及返回值
JQuery中的$.post()函数 先看一个例子:$.post("adduser.ashx", { "name": name, "sex" ...
- 2012年长春网络赛(hdu命题)
为迎接9月14号hdu命题的长春网络赛 ACM弱校的弱菜,苦逼的在机房(感谢有你)呻吟几声: 1.对于本次网络赛,本校一共6名正式队员,训练靠的是完全的自主学习意识 2.对于网络赛的群殴模式,想竞争现 ...
- Java集合框架---重构设计
面向接口编程: 接口类型 变量 =new 实现类(); List list=new ArrayList(); --------------------------------------- List ...
- Java 线程的通讯--生产者和消费者
package 生产者和消费者; //消费者 public class Customer implements Runnable { private Share_resources rescource ...
- DELPHI XE5 UP2 运行IOS 遇到 Wrapper init failed: (null)问题的解决办法
一.问题表现: 在MAC OSX(10.9.2)上安装了比较新的XCODE5.1 和COMMAND LINE TOOLS 在DELPHI XE5 UP2上放了一个按钮,输出到MAC OSX上,出现: ...
- timer实现Grid自动换行(连续相同的id跳到下一行)
private { Private declarations } FRow: Integer; procedure SetRow(const Value: Integer); public { Pub ...
- 严选 Android 路由框架优化(上篇)
0 背景 早前严选 Android 工程,使用原生 Intent 方式做页面跳转,为规范参数传递,做了编码规范,使用静态方法的方式唤起 Activity public static void star ...