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 ...
随机推荐
- mysql中如何不重复插入满足某些条件的重复的记录的问题
最近在项目中遇到了这样的一个问题“: 在mysql数据库中需要每次插入的时候不能插入三个字段都相同的记录.在这里使用到了 insert into if not exists 和insert igno ...
- javascript使用技巧总结,不断更新...
1.使用a标签来获得当前页面相对地址的绝对地址 function getAbsoluteUrl(url){ var a; if(!a) a = document.createElement('a'); ...
- 27-拓扑排序-poj1094
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- CentOS双网卡双IP设置
CentOS双网卡双IP设置 系统环境:CentOS Linux 网络环境: 两个IP地址,192.168.0.10和10.10.30.2,掩码是255.255.255.0,这两个子网的网关地址分别是 ...
- 1、python与ipython的下载与安装
1.ipython的下载与安装 下载链接: wget -c https://github.com/downloads/ipython/ipython/ipython-0.13.1.tar.gz ##下 ...
- Java——操作Excel表格,读取表格内容
JAVA EXCEL API:是一开放源码项目,通过它Java开发人员可以读取Excel文件的内容.创建新的Excel文件.更新已经存在的Excel文件.使用该API非Windows操作系统也可以通过 ...
- oracle使用PLSQL免安装客户端
2. 下载Oracle Instant Client (32-bit) 只需要下载instantclient-basic-nt-11.2.0.3.0.zip就可以了,其它的都是一些根据不同需要扩展的包 ...
- IDEA小技巧:添加代码快捷方式
非常怀恋eclipse的的代码快捷方式tryc,今天给IDEA也添加了一个
- 第三周Linux编程实例练习
通过以下程序来练习 head.h # ifndef HEAD_H #define HEAD_H #include <stdio.h> int add(int,int); int sub(i ...
- MongoDB整理笔记のReplica oplog
主从操作日志oplog MongoDB的Replica Set架构是通过一个日志来存储写操作的,这个日志就叫做"oplog".oplog.rs是一个固定长度的capped coll ...