leetcode day6
【13】Roman to Integer
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
罗马数字转换成阿拉伯数字
Subscribe to see which companies asked this question
思路:
罗马数字的对应关系:I(1)、V(5)、X(10)、L(50)、C(100)、D(500)、M(1000)
1~9 : String[] num1 = {"I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
10~90: String[] num2 = {"X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
100~ 900: String[] num3 = {"C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
1000、2000、3000 : String[] num4 = {"M", "MM", "MMM"};
通过观察各个数字的规律可以发现,两个不同的数字组合时,位于大数的后面时就作为加数;位于大数的前面就作为减数,例如Ⅲ=3,Ⅳ=4,Ⅵ=6,ⅩⅨ=19,ⅩⅩ=20,ⅩLⅤ=45,MCMⅩⅩC=1980
所有的数字组合中一共有6个这样的数字组合:"IV"、"IX"、"XL"、"XC"、"CD","CM",最后要求的罗马数字其实就是简单的每一位映射出来的数字相加即可,但是对于这6个数字要特殊处理下(使用前6个if从句),首先要减去自身(抵消掉第二个for循环里单个数字),然后让位于它后面的大数减去自身,所以需要减去自身两次
public int romanToInt(String s) {
int sum=0;
if(s.indexOf("IV")!=-1){sum-=2;}
if(s.indexOf("IX")!=-1){sum-=2;}
if(s.indexOf("XL")!=-1){sum-=20;}
if(s.indexOf("XC")!=-1){sum-=20;}
if(s.indexOf("CD")!=-1){sum-=200;}
if(s.indexOf("CM")!=-1){sum-=200;} char c[]=s.toCharArray();
int count=0; for(;count<=s.length()-1;count++){
if(c[count]=='M') sum+=1000;
if(c[count]=='D') sum+=500;
if(c[count]=='C') sum+=100;
if(c[count]=='L') sum+=50;
if(c[count]=='X') sum+=10;
if(c[count]=='V') sum+=5;
if(c[count]=='I') sum+=1; } return sum; }
//方法二
public class Solution {
public int romanToInt(String s) {
//:Ⅰ(1)Ⅴ(5)Ⅹ(10)L(50)C(100)D(500)M(1000)
// rules:位于大数的后面时就作为加数;位于大数的前面就作为减数
//eg:Ⅲ=3,Ⅳ=4,Ⅵ=6,ⅩⅨ=19,ⅩⅩ=20,ⅩLⅤ=45,MCMⅩⅩC=1980
//"DCXXI" if(s == null || s.length() == 0) return 0;
int len = s.length();
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
map.put('I',1);
map.put('V',5);
map.put('X',10);
map.put('L',50);
map.put('C',100);
map.put('D',500);
map.put('M',1000);
int result = map.get(s.charAt(len -1));
int pivot = result;
for(int i = len -2; i>= 0;i--){
int curr = map.get(s.charAt(i));
if(curr >= pivot){
result += curr;
}else{
result -= curr;
}
pivot = curr;
}
return result;
}
}
leetcode day6的更多相关文章
- leetcode day6 -- String to Integer (atoi) && Best Time to Buy and Sell Stock I II III
1. String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...
- leetcode每日刷题计划-简单篇day6
突发奇想&胡思乱想的一天 银行家算法证明错了并挂在黑板上的可怜希希 Num 53 最大子序和 Maximum Subarray O(n)的算法实现了,分治法有空补 class Solution ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
随机推荐
- AngularJS vs. jQuery,看看谁更胜一筹
http://www.apjs.net/ http://docs.angularjs.cn/api/ng/function 本文由PHP100中文网编译,转载请看文末的转载要求,谢谢合作!除非特别声明 ...
- ButterKnife-5.1.2.jar(较低版本的ButterKnife)使用方法
ButterKnife注解是编译时注解,Xutils是运行时注解.很明显编译时注解性能更高 一.先配置好环境 1.在工程上右键,选Properties,然后选Java Compiler,选Annota ...
- C# Guid用法
GUID(全局统一标识符)是指在一台机器上生成的数字,它保证对在同一时空 中的所有机器都是唯一的.通常平台会提供生成GUID的API.生成算法很有意思,用到了以太网卡地址.纳秒级时间.芯片ID码和许多 ...
- ural1067 Disk Tree
Disk Tree Time limit: 2.0 secondMemory limit: 64 MB Hacker Bill has accidentally lost all the inform ...
- 硬盘安装Win7、CentOS7双系统
待补充 0.软件 Acronis Disk Director:用来对硬盘分区,将磁盘的一部分格式化成Linux可以识别的ext3格式 Ext2Fsd:因为Windows不能识别ext3格式的文件系统, ...
- CenOS 用PF_RING优化Snort
0.优化顺序 安装PF_RING的kernel模块 安装PF_RING的用户态库 安装Snort的DAQ 安装PF_RING的pfring-daq-module 安装snort 安装PF_RING-a ...
- POJ 2296 Map Labeler
二分答案 + 2-SAT验证,判断正方形是否相交写起来有点烦,思路还是挺简单的. #include<cstdio> #include<cstring> #include< ...
- OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波
http://blog.csdn.net/chenyusiyuan/article/details/8710462 OpenCV学习笔记(27)KAZE 算法原理与源码分析(一)非线性扩散滤波 201 ...
- Android代码资源的国际化
internationalization (国际化)简称 i18n,因为在i和n之间还有18个字符,localization(本地化 ),简称L10n. 一般用语言_地区的形式表示一种语言,如 zh ...
- openssl windows编译 32位&64位
openssl版本:openssl-1.0.0k 64位编译 1.编译环境: openssl-1.0.0a必须用vs2008编译(Open Visual Studio 2008 x64 Cross T ...