LeetCode 13 Roman to Integer(罗马数字转为整数)
int toNumber(char ch) {
switch (ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
return 0;
}
package leetcode_50; /***
*
* @author pengfei_zheng
* 罗马数字转为整数
*/
public class Solution13 {
public int romanToInt(String s) {
int len = s.length();
int ans = toNumber(s.charAt(len-1));
for(int i=len-2;i>=0;i--){
int last = toNumber(s.charAt(i+1));
int first = toNumber(s.charAt(i));
if(first>=last){//后面字符单位小于前面字符单位,加上对应数字
ans+=first;
}
else//减去对应数字
ans-=first;
}
return ans;
}
int toNumber(char ch) {//对应转换规则
switch (ch) {
case 'I': return 1;
case 'V': return 5;
case 'X': return 10;
case 'L': return 50;
case 'C': return 100;
case 'D': return 500;
case 'M': return 1000;
}
return 0;
}
}
LeetCode 13 Roman to Integer(罗马数字转为整数)的更多相关文章
- [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 罗马数字转化成整数
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 ...
- 【LeetCode】Roman to Integer(罗马数字转整数)
这道题是LeetCode里的第13道题. 题目说明: 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1 ...
- 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 罗马数组转整型
描述: 将一个字符串表示的罗马数字转为整数,范围0~3999 解决: 如果后一个比前一个大,则表示减,没什么技巧. map<}, {}, {}, {}, {}, {}, {}}; int rom ...
- 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 解题报告
题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...
- 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 ...
随机推荐
- 多个IoC容器适配器设计及性能测试(Castle.Windsor Autofac Spring.Core)
[转]多个IoC容器适配器设计及性能测试和容器选择 1. 采用的IoC容器和版本 Autofac.2.6.3.862 Castle.Windsor.3.1.0 Spring.Core.2.0.0 2. ...
- PHP上传原理及操作实现
关于PHP上传文件的函数类库,网上有许多封装很完善,大家直接拿来用就可以. 本文章只是说下关于上传原理和简单的上传操作,老鸟就无视了哈^_^~ 还有一些安全性判断比如:服务端限制能接收图片类型的文件, ...
- maven package 与maven install的区别
maven package:会将jar包打包到target下 maven install:将jar包装载到maven仓库,供其他项目使用 项目基于osgi开发的,打包有依赖关系,依赖关系主要是在pom ...
- Redis使用示例及在PHP环境中用redis存储session
在文件夹redis-3.2.0下 1. 启动redis服务 nohup ./src/redis-server redis.conf & 2. 停止服务 #使用客户端 ./src/redis-c ...
- 获取微信小程序源码
https://blog.csdn.net/aaron9185/article/details/80576183 http://lrdcq.com/me/read.php/66.htm https:/ ...
- Linux中/etc/resolv.conf文件简析
https://blog.csdn.net/lcr_happy/article/details/54867510
- POJ 1459 && ZOJ 1734--Power Network【最大流dinic】
Power Network Time Limit: 2000MS Memory Limit: 32768K Total Submissions: 25108 Accepted: 13077 D ...
- 第四章 TCP粘包/拆包问题的解决之道---4.1---
4.1 TCP粘包/拆包 TCP是一个“流”协议,所谓流,就是没有界限的一串数据.TCP底层并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行包的划分,所以在业务上认为,一个完整的包可 ...
- python爬虫系列:做一个简单的动态代理池
自动 1.设置动态的user agent import urllib.request as ure import urllib.parse as upa import random from bs4 ...
- 【Python】Linux Acanoda PySpark Spark
1.安装 Acanoda 2.安装 Spark和Scala 3.安装 PySpark 4.将Spark的Python目录拷贝至 Acanoda目录下 5.安装py4j,切换anaconda中bin目 ...