问题描述:

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的更多相关文章

  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 ,即 ...

  2. Leetcode 13. Roman to Integer(水)

    13. Roman to Integer Easy Roman numerals are represented by seven different symbols: I, V, X, L, C, ...

  3. [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 ...

  4. 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 ...

  5. LeetCode 13. Roman to Integer(c语言版)

    题意: Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value ...

  6. LeetCode 13 Roman to Integer 解题报告

    题目要求 Roman numerals are represented by seven different symbols: I, V, X, L, C, Dand M. Symbol Value ...

  7. [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 ...

  8. [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 ...

  9. [leetcode] 13. Roman to Integer

    如果某一个字母代表的数字大于上一个字母代表的数字小,那么加上这个数字,否则,减去两倍的前一个数字,然后加上这一位数字. public class Solution { private static c ...

随机推荐

  1. sort +awk+uniq 统计文件中出现次数最多的前10个单词

    实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

  2. swift基础--字符串

    (1)遍历 (2)长度 (3)拼接 (4)插值 (5)大小写 (6)trim (7)split ……等等 var a = "你好" var b = String() a.isEmp ...

  3. 团体程序设计天梯赛-练习集L1-017. 到底有多二

    L1-017. 到底有多二 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一个整数“犯二的程度”定义为该数字中包含2的个数与其 ...

  4. 定长内存池之BOOST::pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  5. 如何提高多线程程序的cpu利用率

    正如大家所知道的那样,多核多cpu越来越普遍了,而且编写多线程程序也是件很简单的事情.在Windows下面,调用CreateThread函数一次就能够以你想要的函数地址新建一个子线程运行.然后,事情确 ...

  6. IDS 日志分析

    [http://blog.csdn.net/cnbird2008/article/details/5792626] General Approach通用方法1. Identify which log ...

  7. Grok debugger

    http://www.cnblogs.com/vovlie/p/4227027.html http://it.taocms.org/10/5802.htm

  8. CF135A Replacement

    http://codeforces.com/problemset/problem/135/A 题意 : 我能说我卡在这个题的题意上很久吗.....这个题就是在数组里找一个数,然后找另一个数把他替换掉, ...

  9. 李洪强iOS开发之代理

    如果A想让控制器B为他做事情 用代理的话 首先: 在A的.h文件中:  其次A的.m中 在控制器的.m文件中: 还是在控制器B的.m文件中 在A初始化的那一刻设置控制器B为A的代理 在B的.m中实现代 ...

  10. [codility]Min-abs-sum

    https://codility.com/demo/take-sample-test/delta2011/ 0-1背包问题的应用.我自己一开始没想出来.“首先对数组做处理,负数转换成对应正数,零去掉, ...