题目描述:

Given a roman numeral, convert it to an integer.

解题分析:

这道题只要百度一下转换的规则,然后着这解释写代码即可。实现上并没有什么难度,直接看代码即可

具体代码:

 public class Solution {
public int romanToInt(String s){
int[] value={1000,500,100,50,10,5,1};
char[] array ="MDCLXVI".toCharArray(); int sum=0;
for(int i=0;i<s.length();i++){
char ch=s.charAt(i);
int index=findIndex(array, ch);
if(index==2||index==4||index==6){
if(i+1<s.length() && s.charAt(i+1)== array[index-1]){
sum=sum+value[index-1]-value[index];
i++;
}
else if(i+1<s.length() && s.charAt(i+1)== array[index-2]){
sum=sum+value[index-2]-value[index];
i++;
}
else{
sum+=value[index];
}
}
else{
sum+=value[index];
}
}
return sum;
}
public static int findIndex(char[] array,char ch){
for(int i=0;i<array.length;i++){
if(array[i]==ch)
return i;
}
return -1;
}
}

【leetcode】13. Roman to Integer的更多相关文章

  1. 【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 ...

  2. 【LeetCode】13. Roman to Integer 罗马数字转整数

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  3. 【一天一道LeetCode】#13. Roman to Integer

    一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...

  4. 【LeetCode】013. Roman to Integer

    Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 t ...

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

  6. 《LeetBook》leetcode题解(13):Roman to Integer[E]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. 【leetcode❤python】13. Roman to Integer

    #-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...

  8. LeetCode题解(13)--Roman to Integer

    https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...

  9. 【LeetCode】7、Reverse Integer(整数反转)

    题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...

随机推荐

  1. [Angular2 Form] Group Inputs in Angular 2 Forms with ngModelGroup

    The ngModelGroup directive allows you to group together related inputs so that you structure the obj ...

  2. FreeMarker中文API手冊(完整)

    FreeMarker概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写 FreeMarker被设计用来生成HTML Web页面,特别是基于MVC模式的应用 ...

  3. Linux文件系统的几个性能测试软件小结

    曾经测试Linux系统下的分布式集群系统的性能,使用了一些测试软件,公司让我给部门同事做一次基础培训,于是翻看以前所写的记录资料挑选了其中几个,所记之处并不完全,只记录使用的功能. 1.Iozone ...

  4. SVM多分类

    http://www.matlabsky.com/thread-9471-1-1.htmlSVM算法最初是为二值分类问题设计的,当处理多类问题时,就需要构造合适的多类分类器.目前,构造SVM多类分类器 ...

  5. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  6. 记录一下bing的图片 - 升级版冰糖葫芦

    记录一下bing的图片 - 升级版冰糖葫芦

  7. LeetCode35 Search Insert Position

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. 使用idea创建maven的web项目

    如果是第一次打开软件直接点击 Create New Project ,如果之前已经打开过项目了,需要点击菜单中 File → New Project … 如下图: 选择 Maven module ,输 ...

  9. Amazon 开始接受 Windows 礼品卡预订

    在 8 月微软虚拟货币系统 Microsoft Points 已经正式被真实货币替代,但目前,配套真实货币系统将推出的礼品卡还并没有开始销售.Amazon 上的一则预订显示“Windows 礼品卡”( ...

  10. iOS - UI - UIScrollView

    1.UIScrollView 滚动视图 // 滚动视图 UIScrollView* scrollView = [[UIScrollView alloc]initWithFrame:self.view. ...