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. “全栈2019”Java第七十章:静态内部类详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  2. 洛谷P3783 [SDOI2017]天才黑客(前后缀优化建图+虚树+最短路)

    题面 传送门 题解 去看\(shadowice\)巨巨写得前后缀优化建图吧 话说我似乎连线段树优化建图的做法都不会 //minamoto #include<bits/stdc++.h> # ...

  3. MySQL直接导出CSV文件,并解决中文乱码的问题

    需求: 需要导出hr_users 表中的部分字段的数据,以前是用PHP写脚本,然后导出CSV文件. 在MySQL中,它自己就能导出CSV文件 ,只不过是有如下几个问题需要大家解决. 1. 生成文件不成 ...

  4. Java 之多态

    Java 之多态(包含封装) 基础知识: Java 在处理基本数据类型(例如int ,char,double)时,都是采用按值传递的方式执行,除此之外的其它类型都是按引用传递的方式执行.对象除了在函数 ...

  5. springboot 项目中 使用 原型 模式 实现每一次 都获取不同的实例

    直接 上代码:

  6. python requests 包 使用

    1: 发送带 cookie 的 请求 resp = requests.get(self.url_item_list_first_page, cookies=self.cookies) >> ...

  7. 海思hi35xx 开发学习(3):视频输入

    视频输入(VI)模块实现的功能:通过 MIPI Rx(含 MIPI 接口.LVDS 接口和 HISPI 接口),SLVS-EC,BT.1120,BT.656,BT.601,DC 等接口接收视频数据.V ...

  8. weex 初始化

    文档教程 一. 已有项目集成weex, 有时候会报错, 因为sdk中用到了socket pod 'WeexSDK' pod 'SocketRocket' 二. 在app启动方法 -didFinish ...

  9. 【医学影像】《Identifying Medical Diagnoses and Treatable Diseases by Image-Based Deep Learning》论文笔记

    这篇论文的作者是张康教授为首的团队,联合国内外众多医院及科研机构,合力完成,最后发表在cell上,实至名归. 从方法的角度上来说,与上一篇博客中的论文很相似,采用的都是InceptionV3模型,同时 ...

  10. centos的基本命令04

    零:简述linux的文档目录结构 linux的文档目录是一个树形结构,操作的时候表现为以 / 开头的树形结构,/也是系统 的最顶端,也就是linux的root,也是linux系统的文件系统的入口. 他 ...