Java [leetcode 13] Roman to Integer
问题描述:
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
解题思路:
上一题反过来就可以了,其实觉得这两道题很没有意思。
代码如下:
public class Solution {
public int romanToInt(String s) {
int result;
if (s == null || s.length() == 0)
return 0;
result = toNumber(s.charAt(0));
for (int i = 1; i < s.length(); i++) {
if (toNumber(s.charAt(i - 1)) < toNumber(s.charAt(i))) {
result += toNumber(s.charAt(i)) - 2 * toNumber(s.charAt(i - 1));
} else {
result += toNumber(s.charAt(i));
}
}
return result;
}
private 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;
}
}
Java [leetcode 13] Roman to Integer的更多相关文章
- 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 罗马数字转化成整数
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 - 思考if-else与switch的比较 - ( C++ ) - 解题报告
1.题目: 原题:Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...
- LeetCode 13. Roman to Integer(c语言版)
题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...
- 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罗马数字转整数
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 1 t ...
- [leetcode] 13. Roman to Integer
如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...
随机推荐
- 【转】 Linux Core Dump 介绍
=============================================================== Linux core dump的祥细介绍和使用 =========== ...
- CSS 将按钮转成超链接样式
一.将按钮转成超链接样式 .GoStyle { color: #0c5fc4; background-color: #FFFFFF; ...
- 隐藏和显示效果js动画
<div id='ctt' style='margin-left: 50px; color: white'> <input type="button ...
- 获取网页上数据(图片、文字、视频)-b
Demo地址:http://download.csdn.net/detail/u012881779/8831835 获取网页上所有图片.获取所有html.获取网页title.获取网页内容文字... . ...
- 你所不知道的ref
在c#中有个关键字叫ref,它的作用是使参数按引用传递,基本用法如下: class RefExample { static void Method(ref int i) { i = ; } stati ...
- select count(*)和select count(1)的区别 (转)
A 一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键 ...
- C#学习笔记(一)
1.cmd运行devenv启动VS. 2.解决方案:公司 项目:部门 类:员工 3.右边的解决方案管理器:会自动隐藏,想让他固定的话,就点击关闭按钮中间的“自动隐藏”:可以拖动到上下左右,当出现阴影的 ...
- XSS与CSRF两种跨站攻击比较
XSS:跨站脚本(Cross-site scripting) CSRF:跨站请求伪造(Cross-site request forgery) 在那个年代,大家一般用拼接字符串的方式来构造动态SQL 语 ...
- WPF MultiDataTrigger
huhu <Style x:Key="Cell" TargetType="{x:Type Button}"> <Setter Property ...
- POJ2526+简单几何
题意:给定的这些点是否有一个对称中心. PS:我写得有点啰嗦.. 就是把小的x和大的x进行匹配. #include<stdio.h> #include<algorithm> # ...