Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

这一道题的思路是,先把特殊情况表现在结果里。然后再将字符串转化为字符串数组,用for循环得出最后的结果。

从题目中可以看出,当I, X, C放在其他数前面的时候,他们代表的是减去自己的数值。因为在后面for循环的过程中,它们还要被加一遍自己的值,所以在一开始的时候应该减去两倍的自己的值。

需要将字符串转化为字符串数组的原因是,indexOf()只能判断字符串中有没有这个字符,不能判断出现了几次。而在罗马数字中,一个字符可能出现多次。但是在罗马数字中“IV”,“IX”这些特殊情况不可能相同的情况在一个字符串中出现两次,所以在计算特殊情况的时候可以用indexOf()。

 class Solution {
public int romanToInt(String s) {
int sum=0;
if(s.indexOf("IV")!=-1) sum=sum-2;
if(s.indexOf("IX")!=-1) sum=sum-2;
if(s.indexOf("XL")!=-1) sum=sum-20;
if(s.indexOf("XC")!=-1) sum=sum-20;
if(s.indexOf("CD")!=-1) sum=sum-200;
if(s.indexOf("CM")!=-1) sum=sum-200; char[] arr=s.toCharArray();
for(int i =0;i<s.length();i++){
if(arr[i]=='I') sum=sum+1;
if(arr[i]=='V') sum=sum+5;
if(arr[i]=='X') sum=sum+10;
if(arr[i]=='L') sum=sum+50;
if(arr[i]=='C') sum=sum+100;
if(arr[i]=='D') sum=sum+500;
if(arr[i]=='M') sum=sum+1000;
}
return sum;
}
}

这道题主要用了两个字符串函数 indexOf() 和  toCharArray()。

indexOf() 方法在字符串中查找子字符串出现的位置,如过存在返回字符串出现的位置(第一位为0),如果不存在返回 -1。

toCharArray() 方法将字符串转化为字符串数组。如果原字符串中有分隔符,例如逗号,空格等,可以用split(string) 方法将字符串通过指定分隔符分割为数组。

13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy的更多相关文章

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

  2. [LintCode] Roman to Integer 罗马数字转化成整数

    Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...

  3. [LeetCode] Roman to Integer 罗马数字转化成整数

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

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

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

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

  6. 13 Roman to Integer(罗马数字转int Easy)

    题目意思:罗马数字转int 思路:字符串从最后一位开始读,IV:+5-1 class Solution { public: int romanToInt(string s) { map<char ...

  7. LeetCode 13 Roman to Integer(罗马数字转为整数)

    题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description   int toNumber(char ch) { switc ...

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

  9. leetCode练题——13. Roman to Integer

    1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...

随机推荐

  1. [Swift实际操作]八、实用进阶-(5)通过间接代理进行对象间的消息传递

    本文将为你演示,如何通过简介代理的方式,进行对象间的消息传递在左侧的项目导航区,打开自定义视图的代码文件(CustomView.swift). import UIKit class CustomVie ...

  2. svn提交新文件夹同时不需要更新全部上级目录

    关于svn的指定目录指定位置更新:当在提交了新建的目录后可以使用 a)  在需要更新的上级目录上单击右键 在延伸菜单中选择 b)  弹出对话框中选择,check repository c)  新添加的 ...

  3. XML标签属性

    0. 内容索引 1. XML序列化与反序列化 2. 相关的常用Attribute 3. 相关的全部Attribute 4. XML序列化答疑 5. 高级议题 1. XML序列化与反序列化 // OBJ ...

  4. 题目1021:统计字符(hash简单应用)

    问题来源 http://ac.jobdu.com/problem.php?pid=1021 问题描述 每次输入两个字符串,统计第一个字符串中的每个字符在第二个字符串中出现的次数. 问题分析 太明显了, ...

  5. [转] linux nc命令

    [From] https://blog.csdn.net/freeking101/article/details/53289198 NC 全名 Netcat (网络刀),作者是 Hobbit & ...

  6. python 模块和包以及他们的导入关系

    一 模块 1 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用python编 ...

  7. 整理的最全 python常见面试题

      整理的最全 python常见面试题(基本必考)① ②③④⑤⑥⑦⑧⑨⑩ 1.大数据的文件读取: ① 利用生成器generator: ②迭代器进行迭代遍历:for line in file; 2.迭代 ...

  8. setSelectionRange方法解决光标错位问题

    inputElement.setSelectionRange(value.length, value.length);

  9. Python与C相互调用、编译

    因为最近学习Boost::python的缘故,想尝试下不同语言之间的相互编译. 参考资料:http://blog.csdn.net/joliny/article/details/2457197. 很吃 ...

  10. idea中运行maven安装jar包到本地仓库跳过test