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 ...
随机推荐
- 【Php】数组遍历,foreach, each, trim()
<?php $iplist = "122.224.251.154|192.168.2.138|192.168.2.12"; echo $_SERVER['REMOTE_ADD ...
- cocos2d - Changing the image of a CCSprite
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:@"new_image_name"]; [spri ...
- scala实现相邻两个元素挑换位置的代码,哈哈
import scala.math._ import breeze.plot._ import breeze.linalg._ import scala.collection.mutable.Arra ...
- hive cst 时间转换
select from_unixtime(unix_timestamp(r.collecttime,'EEE MMM dd HH:mm:ss zzz yyyy'),'yyyy-MM-dd HH:mm: ...
- Spring Boot 处理 REST API 错误的正确姿势
摘要:如何正确的处理API的返回信息,让返回的错误信息提供更多的含义是一个非常值得做的功能.默认一般返回的都是难以理解的堆栈信息,然而这些信息也许对于API的客户端来说有可能并没有多大用途,并没有多大 ...
- php5.4转5.3被替换的函数
今天服务器由于业务需求,需要换成php5.4版本,以前使用的5.3,有些函数过期,导致了许多问题 1.ereg() 使用 preg_match() 替代 int preg_match ( string ...
- SmbException: 0xC000007F
The error code 0xC000007F means: NT_STATUS_DISK_FULL There is not enough space on the disk. https: ...
- [Converge] Backpropagation Algorithm
Ref: CS231n Winter 2016: Lecture 4: Backpropagation Ref: How to implement a NN:中文翻译版本 Ref: Jacobian矩 ...
- ECharts 3 -- gauge表盘的配置项
绘制一个简单的表盘图表 在绘图前我们需要为 ECharts 准备一个具备高宽的 DOM 容器. <body> <!-- 为 ECharts 准备一个具备大小(宽高)的 DOM --& ...
- vuejs时间格式化
date.js export function formatDate(date, fmt) { if (/(y+)/.test(fmt)) { fmt = fmt.replace(RegExp.$1, ...