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. gitlab迁移版本库(保留原版本库的所有内容)

    如果你想从别的 Git 托管服务那里复制一份源代码到新的 Git 托管服务器上的话,可以通过以下步骤来操作. 1) 从原地址克隆一份裸版本库,比如原本托管于 GitHub git clone --ba ...

  2. 不用split调转字符串

  3. 关于java中String的用法

    在java 中String存在许多的基本函数,接下来了解一下这些函数的基本用法 String.equals用法(这个用法比较难) String类中的equals()方法: public boolean ...

  4. SpringBoot 启动的时候提示 Field *** in *** required a bean named 'entityManagerFactory' that could not be found.

    错误截图 后面发现原来和入口类代码有关. //@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard",& ...

  5. Boost内存池使用与测试

    目录 Boost内存池使用与测试 什么是内存池 内存池的应用场景 安装 内存池的特征 无内存泄露 申请的内存数组没有被填充 任何数组内存块的位置都和使用operator new[]分配的内存块位置一致 ...

  6. editplus 编辑 php双击选中变量问题

    windows下,在很多地方双击鼠标左键可以选中一个连续的英文字符串. 在editplus 编辑器里可以双击选中一个变量,方便了编程,但是使用phptools(php.stx)增强语法插件后,在一个变 ...

  7. springboot设置日志级别时报错

    配置springboot日志,输出级别为info,运行时报错: Caused by: org.springframework.boot.context.properties.bind.BindExce ...

  8. [转] watch 命令使用(linux监控状态)

    [From] https://jingyan.baidu.com/article/495ba841c5a31738b30eded4.html 可以使用watch 命令设置执行间隔,去反复间隔一条命令或 ...

  9. Oracle 创建数据库卡死在85%

    Oracle 创建数据库卡死在85%处理方法 1.首先用改工具,删除数据库 2.对应的目录 3.重启服务器(本次尝试有重启,在有其他数据库在跑的情况下,可以试试不重启)

  10. QueryRunner(DBUtils) 结果集实例

    转自:http://www.cnblogs.com/myit/p/4272824.html#   单行数据处理:ScalarHandler    ArrayHandler    MapHandler  ...