【leetcode】13. Roman to Integer
题目描述:
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的更多相关文章
- 【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 ...
- 【LeetCode】13. Roman to Integer 罗马数字转整数
题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...
- 【一天一道LeetCode】#13. Roman to Integer
一天一道LeetCode系列 (一)题目 Given a roman numeral, convert it to an integer. Input is guaranteed to be with ...
- 【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 ...
- 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 ...
- 《LeetBook》leetcode题解(13):Roman to Integer[E]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【leetcode❤python】13. Roman to Integer
#-*- coding: UTF-8 -*-#从前向后遍历罗马数字,#如果某个数比前一个数小,则加上该数.反之,减去前一个数的两倍然后加上该数###-----技术规则-----#----------- ...
- LeetCode题解(13)--Roman to Integer
https://leetcode.com/problems/roman-to-integer/ 原题: Given a roman numeral, convert it to an integer. ...
- 【LeetCode】7、Reverse Integer(整数反转)
题目等级:Easy 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 O ...
随机推荐
- iOS 在类实现定义中声明成员变量的怪异方式
WebGL 规范(WebGL Specification) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&quo ...
- 高级Swing——列表
1. 列表 1.1 JList构件 JList可以将多个选项放置在单个框中.为了构建列表框,首先需要创建一个字符串数组,然后将这个数组传递给JList构造器. String[] words= { &q ...
- 基于linux2.6.38.8内核zImage文件的自解压详解
转载:http://blog.csdn.net/wavemcu/article/details/7270439 ******************************************** ...
- Linux根文件系统的制作
转载:http://www.cnblogs.com/hnrainll/archive/2011/06/09/2076655.html 1. 根文件系统 文件系统是包括在一个磁盘(包括光盘.软盘.闪盘及 ...
- 关于c#调用c/c++ dll遇到的问题总结
前段时间公司做了个winform程序,需要调用c 的dll去读取卡号的程序,期间遇到些问题,下面来分享下 一.dll路径问题 相信很多开发者都会遇到这个问题,我总结了下我现在有3总方式去解决这个问题: ...
- IOS 给图片添加水印(文字)
有时候上传图片要加唯一标识,简单的就是添加一个水印.这里水印我们讲文字,可以是当前系统时间.坐标.地理位置等 原理就是把一个字符串写到图片上,并且字(font)的大小由图片大小控制. 以下是封装好的一 ...
- iOS 画图讲解2
1.图片水印 //layer上下文只能显示在drawRect里 //当开启上下文时,绘制图形即可在viewDidLoad中实现 //位图的上下文 //UIGraphicsBeginImageConte ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...
- html + js 右 点击 弹出 菜单
页面 引用jar 包 <link rel="stylesheet" href="../../style/zui.min.css" type="t ...
- Sublime Text shift+ctrl妙用
1 :按住shift+ctrl然后按←或→可快速选中一行中的某一部分,相当于双击鼠标选中. 当你想在代码末尾加注释的话,这个方法很好用 输入文字->光标移到文字末尾->按住shift+ct ...