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. oracle无监听程序的解决方法(PLSQL)Oracle ORA12514 监听程序当前无法识别连接描述符中请求的服务

    \PLSQL\instantclient_11_2 listener.ora # listener.ora Network Configuration File: E:\software\PLSQL\ ...

  2. windows server2008 r2安装DNS服务器

    1.开始->管理工具->服务器管理器 2.角色->添加角色 3.服务器角色->DNS服务器 4.一直点击下一步,直至安装完成. (确认步骤时会提示,可能会需要重启服务器) 安装 ...

  3. 洛谷P2462 [SDOI2007]游戏(哈希+最长路)

    题面 传送门 题解 我们把字符的出现次数哈希起来,然后把每个点向能在它之后的点连边.那么这显然是一个\(DAG\),直接求最长路就行了 //minamoto #include<bits/stdc ...

  4. Python编程中 re正则表达式模块 介绍与使用教程

    Python编程中 re正则表达式模块 介绍与使用教程 一.前言: 这篇文章是因为昨天写了一篇 shell script 的文章,在文章中俺大量调用多媒体素材与网址引用.这样就会有一个问题就是:随着俺 ...

  5. Python 标准库之 xml.etree.ElementTree

    Python 标准库之 xml.etree.ElementTree Python中有多种xml处理API,常用的有xml.dom.*模块.xml.sax.*模块.xml.parser.expat模块和 ...

  6. css里面鼠标的形状-----》easyui哪点事

    用css控制鼠标样式的语法如下:<span style="cursor:*">文本或其它页面元素</span>把 * 换成如下15个效果的一种: 下面是对这 ...

  7. 51nod1847 奇怪的数学题 (Min_25筛+第二类斯特林数)

    link \(\sum_{i=1}^n\sum_{j=1}^n\mathrm{sgcd}(i,j)^k=\sum_{p=1}^ns(p)^k\sum_{i=1}^n\sum_{j=1}^n[\gcd( ...

  8. 1.jQuery入口函数

    <!--注意,如果需要对ie67兼容,我们可以使用原生低版本的jquery 比如说jquery-1.12.4.js--> <!DOCTYPE html> <html la ...

  9. python学习,day1作业:设计一个三级菜单

    设计一个三级菜单,要求能够下钻,能够推出,比较笨的方法 # coding=utf-8 # Author: RyAn Bi data = { 'beijing':{ 'changping':{ 'wud ...

  10. flutter dup get

    有很多 flutter library 中有两种导入方式 with dup: dup get 这种导入方式是需要依赖于 Dart SDK 下载 Dart SDK 地址:http://www.gekor ...