题目描述:

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. iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能

    UItableView分组功能 class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableVi ...

  2. 百度UEditor组件出现Parameters: Invalid chunk &#39;&#39; ignored警告的分析

    使用百度UEditor在线编辑器组件时,出现Parameters: Invalid chunk '' ignored的警告,之前的项目使用却没有.两个项目的环境应该是一样的. 没有时间去对照两项目使用 ...

  3. epoll的使用

      http://blog.csdn.net/ljx0305/article/details/4065058 epoll - I/O event notification facility 在linu ...

  4. The Socket API, Part 3: Concurrent Servers

    转:http://www.linuxforu.com/2011/10/socket-api-part-3-concurrent-servers/ By Pankaj Tanwar on October ...

  5. 嵌入式设备上运行AllJoyn注意事项

    1. 交叉编译AllJoyn库.编译成功后的文件位于:alljoyn-3.3.0-src\build\linux\arm\debug\dist\目录下: 2. 程序要使用AllJoyn,必须要启动al ...

  6. Data Struture 之 指针

    指针是C语言中广泛使用的一种数据类型. 运用指针编程是C语言最主要的风格之一.利用指针变量可以表示各种数据结构: 能很方便地使用数组和字符串:  并能象汇编语言一样处理内存地址,从而编出精练而高效的程 ...

  7. maven安装仓库中不存在的jar包

    这里以ojdbc6.jar作为案例 首先我的ojdbc6.jar放在D盘的根目录D:\ojdbc6.jar 然后我们打开cmd命令窗口,运行命令:mvn install:install-file -D ...

  8. 典型的字符串处理代码(page50)

    Page50: public class TypicalString{//典型的字符串处理代码 public static boolean isPlalindrom(String s){//判断字符串 ...

  9. 日期类型的input元素设置默认值为当天

    html文件:<input name="" type="date" value="" id="datePicker" ...

  10. CF Soldier and Cards (模拟)

    Soldier and Cards time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...