13. Roman to Integer 罗马数字转化为阿拉伯数字(indexOf ()和 toCharArray())easy
Roman numerals are represented by seven different symbols: I, V, X, L, C, D 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:
Ican be placed beforeV(5) andX(10) to make 4 and 9.Xcan be placed beforeL(50) andC(100) to make 40 and 90.Ccan be placed beforeD(500) andM(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的更多相关文章
- [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 ...
- [LintCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. The answer is guaranteed to be within the range fro ...
- [LeetCode] Roman to Integer 罗马数字转化成整数
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 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罗马数字转整数
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- 13 Roman to Integer(罗马数字转int Easy)
题目意思:罗马数字转int 思路:字符串从最后一位开始读,IV:+5-1 class Solution { public: int romanToInt(string s) { map<char ...
- LeetCode 13 Roman to Integer(罗马数字转为整数)
题目链接 https://leetcode.com/problems/roman-to-integer/?tab=Description int toNumber(char ch) { switc ...
- 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 ,即 ...
- leetCode练题——13. Roman to Integer
1.题目13. Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
随机推荐
- 百度直接搜IP可以查看本机外网IP
百度直接搜IP可以查看本机外网IP ipconfig在控制台查看的是内网IP
- 用最短的payload绕过WAF(入门)
本文作者:jishuzhain <font color=green>想绕过一个WAF,我们可以用最短的payload来做,这里只是基础示例,望各位大佬勿喷,小弟在此谢过.</font ...
- 快速启动工具Rulers 4.1
Rulers 4.1 Release 360云盘 https://yunpan.cn/cSbq5nx9GVwrJ 访问密码 0532 百度云 http://pan.baidu.com/s/1czCNR ...
- mysql主从同步错误,提示The server quit without updating PID file
在安装完lnmp后,启动mysqld失败,提示 [root@centos-6 ~]# service mysqld start Starting MySQL [确定][root@centos-6 ~] ...
- Linux系统如何迁移至LVM磁盘
今天遇到一个问题,算是比较严重的把.就是要把当前系统转移到 LVM 卷里面去,下面有一些发生过程介绍. 不感兴趣可以直接跳过,看实战部分<如何迁移系统至LVM卷> 朋友今天突然找我,说是要 ...
- php 的加法
无意间看到了php中关于加,减,乘,除 的计算方法 这里 http://lxr.php.net/source/xref/PHP-5.6/Zend/zend_operators.h#596 static ...
- [Ruby]Unzipping a file using rubyzip
link: http://www.markhneedham.com/blog/2008/10/02/ruby-unzipping-a-file-using-rubyzip/ require 'ruby ...
- sql 简单语法
1.数据库操作 create database student_info -- 创建数据库 drop database student_info -- 删除数据库 2.表操作 -- 创建表 creat ...
- JS中将XML转为JSON对象
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script> <scr ...
- Delphi XE TStringBuilder
function T_SunnySky_SDK.JoinItems(AParamDic: TDictionary<string, string>): string; var sb : TS ...