题目:罗马数字转换

题目难度:easy

题目内容: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.

翻译

例如,2在罗马数字中被写成II,就是两个加在一起。12是写成,XII,也就是X+II。数字二十七是二十七,也就是XX+V+II。

罗马数字通常从左到右写得最大。然而,4的数字不是IIII。相反,数字4被写成IV,因为1在5之前减去它等于4。同样的原则也适用于9号,它被写成IX。有六种情况下使用减法:

I可以在V(5)和X(10)之前放置分别是4和9。

X可以放在L(50)和C(100)之前,分别是40和90。

C可以放置在D(500)和M(1000)之前,分别是400和900。

给定一个罗马数字,把它转换成整数。输入在从1到3999的范围内。

思路

加法直接加,减法只匹配前后两个字符,所以当cur字符小于下一个字符的时候,使用减法。

用一个Map将各个字符与对应值都存进去,即可进行比较和取值

MyCode

     public int romanToInt(String s) {
if (s == null || s.isEmpty()) {
return -1;
} char[] sChar = s.toCharArray();
Map<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 sum=0;
for(int i=0;i<sChar.length-1;i++){
if(map.get(sChar[i]) < map.get(sChar[i+1]))
sum-=map.get(sChar[i]); // 当前字符比下一个小,则总和减去此数字
else
sum+=map.get(sChar[i]); // 否则直接加上
}
return sum+map.get(sChar[sChar.length-1]); // 因为最后一个没判断,并且无后续,所以必然是加
}

结果:Accept

参考答案

  public int romanToInt(String s) {
int nums[]=new int[s.length()];
for(int i=0;i<s.length();i++){
switch (s.charAt(i)){
case 'M':
nums[i]=1000;
break;
case 'D':
nums[i]=500;
break;
case 'C':
nums[i]=100;
break;
case 'L':
nums[i]=50;
break;
case 'X' :
nums[i]=10;
break;
case 'V':
nums[i]=5;
break;
case 'I':
nums[i]=1;
break;
}
}
int sum=0;
for(int i=0;i<nums.length-1;i++){
if(nums[i]<nums[i+1])
sum-=nums[i];
else
sum+=nums[i];
}
return sum+nums[nums.length-1];
}

 答案思路:此处直接将原来的字符数组(字符串)转换成相对应的int数组,这样就不需要一个map了,相对来说这种方法的访问更加简便,但是增加了一轮算法复杂度,不过map的访问要比直接数组访问多一个步骤,所以两者复杂度算下来可能差不多都是O(2n)的样子,而答案这种操作更加简单一些,可以借鉴。

LeetCode第[13]题(Java):Roman to Integer的更多相关文章

  1. LeetCode专题-Python实现之第13题:Roman to Integer

    导航页-LeetCode专题-Python实现 相关代码已经上传到github:https://github.com/exploitht/leetcode-python 文中代码为了不动官网提供的初始 ...

  2. LeetCode第[7]题(Java):Reverse Integer 标签:数学

    题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:A ...

  3. LeetCode第[18]题(Java):4Sum 标签:Array

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...

  4. LeetCode第[1]题(Java):Two Sum 标签:Array

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  5. LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2

    题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...

  6. LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  7. leetcode第13题--Roman to Integer

    Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range ...

  8. [LeetCode][Java] Roman to Integer

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  9. LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array

    题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...

随机推荐

  1. 下载flv格式视频

    我们再看flash plaer播放视频时,有些时候需要下载,无奈找不到下载的按钮.这时,我们可以用以下的方式来进行下载. 其它格式估计也是有迹可循,大家仔细看看网页源代码,看到类似于这种地址,看到有相 ...

  2. centos7.3下使用yum 安装pip

    centos下安装pip时失败: No package pip available.Error: Nothing to do 解决方法: 需要先安装扩展源EPEL. EPEL(http://fedor ...

  3. virtualbox mac-debian共享文件夹

    1 这篇笔记所要解决的问题 How to Install VirtualBox Guest Additions in Debian 9 Virtual Machine 不需要管host os的类型. ...

  4. 【Python之路】第二十一篇--Memcached、Redis

    Memcached Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度 ...

  5. iOS开发——生命周期

    为了处理好应用程序的挂起.暂停等情况下的数据保存,或对应添加所需处理,我们必须了解ios生命周期. 但是不要去背去记,做个实验就好. - (BOOL)application:(UIApplicatio ...

  6. Java方法区和永久代

    Java方法区和永久代 目前有三大Java虚拟机:HotSpot,oracle JRockit,IBM J9. JRockit是oracle发明的,用于其WebLogic服务器,IBM JVM是IBM ...

  7. django 表单系统 之 forms.ModelForm

    继承forms.ModelForm类实现django的表单系统 有时,我们在前端定义的<form>表单和后端定义的model结构基本上是一样的,那么我们可以直接在后端定义model后,定义 ...

  8. oradebug工具使用2(转载)

    oradebug的前身是在ORACLE 7时的ORADBX,它可以启动用停止跟踪任何会话,dump SGA和其它内存结构,唤醒ORACLE进程,如SMON.PMON进程,也可以通过进程号使进程挂起和恢 ...

  9. beego——模型(model)

    beego ORM是一个强大的Go语言ORM框架.她的灵感主要来自Django ORM和SQLAlchemy. 已经支持的数据库驱动: MySQL:https://github.com/go-sql- ...

  10. 微信小程序组件swiper

    视图容器swiper:官方文档 Demo Code Page({ data:{ imgUrls: [ 'http://img02.tooopen.com/images/20150928/tooopen ...