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 ...
随机推荐
- 学习笔记——组合模式Composite
组合模式,典型的层次结构. 与装饰器类图相似. 区别在于:装饰器模式是为了在接口中增加方法,而组合模式在于层次元素的叠加. ConcreteComponent就是中间结点,可以包含更多的Concret ...
- ubuntu上的mysql数据库双机备份设置
配置环境: myslq 5.5.3 + ubuntu server 12.04 一.配置MySQL主服务器(192.168.0.1) 1.增加一个账号专门用于同步 1 mysql>grant r ...
- HDU 5723 Abandoned country
题目说每条边权值都不一样,说明最小生成树是唯一的,不存在最小期望这一说. 然后就是先求出最小生成树,随便确定一个根节点,计算出每个点的子树有多少节点,记为c[x]. 指向x的这条边被统计的次数为c[x ...
- hdu4861(游戏)
数论问题:试题 题目没有看懂,但是解题方法感觉的确是非常巧妙的,应该是属于数论一方面的试题. 试题分析: 首先是DouBiNan先取,所以肯定优先选取剩余中值最大的,于是不存在说DouBiNan值小的 ...
- FusionCharts导出图表常见问题(FAQ)汇总---FusionCharts常见问题大全
在前面几篇文章中,我们介绍了FusionCharts生成Flash图表常见问题FAQ以及使用中的一些常见报错及调试/解决方法.本文继续介绍FusionCharts导出图表时的一些常见问题(FAQ). ...
- HDU 1203 I NEED A OFFER! 01背包 概率运算预处理。
题目大意:中问题就不说了 ^—^~ 题目思路:从题目来看是很明显的01背包问题,被录取的概率记为v[],申请费用记为w[].但是我们可以预先做个处理,使问题解决起来更方便:v[]数组保留不被录取的概率 ...
- multipleOutputs Hadoop
package org.lukey.hadoop.muloutput; import java.io.IOException; import org.apache.hadoop.conf.Config ...
- PAT (Advanced Level) 1070. Mooncake (25)
简单贪心.先买性价比高的. #include<cstdio> #include<cstring> #include<cmath> #include<vecto ...
- 深入浅出Ajax(三)
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...
- 第十七篇 --ANDROID DisplayManager 服务解析一
ANDROID从版本4.2开始提供了一个显示管理服务DisplayManagerService,支持多种显示类型的多个显示器的镜像显示,包括内建的显示类型(本地).HDMI显示类型以及支持WIFI D ...