LeetCode第[13]题(Java):Roman to Integer

题目:罗马数字转换

题目难度: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

 1     public int romanToInt(String s) {
 2         if (s == null || s.isEmpty()) {
 3             return -1;
 4         }
 5
 6         char[] sChar = s.toCharArray();
 7         Map<Character, Integer> map = new HashMap<Character, Integer>();
 8         map.put(‘I‘, 1);
 9         map.put(‘V‘, 5);
10         map.put(‘X‘, 10);
11         map.put(‘L‘, 50);
12         map.put(‘C‘, 100);
13         map.put(‘D‘, 500);
14         map.put(‘M‘, 1000);
15
16         int sum=0;
17         for(int i=0;i<sChar.length-1;i++){
18             if(map.get(sChar[i]) < map.get(sChar[i+1]))
19                 sum-=map.get(sChar[i]); // 当前字符比下一个小,则总和减去此数字
20             else
21                 sum+=map.get(sChar[i]); // 否则直接加上
22         }
23         return sum+map.get(sChar[sChar.length-1]); // 因为最后一个没判断,并且无后续,所以必然是加
24     }

结果:Accept

参考答案

 1  public int romanToInt(String s) {
 2     int nums[]=new int[s.length()];
 3     for(int i=0;i<s.length();i++){
 4         switch (s.charAt(i)){
 5             case ‘M‘:
 6                 nums[i]=1000;
 7                 break;
 8             case ‘D‘:
 9                 nums[i]=500;
10                 break;
11             case ‘C‘:
12                 nums[i]=100;
13                 break;
14             case ‘L‘:
15                 nums[i]=50;
16                 break;
17             case ‘X‘ :
18                 nums[i]=10;
19                 break;
20             case ‘V‘:
21                 nums[i]=5;
22                 break;
23             case ‘I‘:
24                 nums[i]=1;
25                 break;
26         }
27     }
28     int sum=0;
29     for(int i=0;i<nums.length-1;i++){
30         if(nums[i]<nums[i+1])
31             sum-=nums[i];
32         else
33             sum+=nums[i];
34     }
35     return sum+nums[nums.length-1];
36 }

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

原文地址:https://www.cnblogs.com/Xieyang-blog/p/8878596.html

时间: 2024-11-07 13:54:58

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

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

题目:Reverse Integer 难度:Easy 题目内容: Given a 32-bit signed integer, reverse digits of an integer. Note:Assume we are dealing with an environment which could only hold integers within the 32-bit signed integer range. For the purpose of this problem, assum

leetcode第13题--Roman to Integer

Problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 遍历一次输入的字符串,如果不满足类似4或者9的就直接加相应的数,否则减去相应的数.其中对应如下”IVXLCDM“对应{1,5,10,50,100,500,1000} class Solution { public: int romanToInt(string s)

[LeetCode][Java] Roman to Integer

题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 题意: 给定一个罗马数字,将其转化为整数. 给定的输入保证在1-3999之间 算法分析: * 罗马数字规则: * 1, 罗马数字共有7个,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). * 罗马数字中没有"0". * 2,

【LeetCode从零单刷】Roman to Integer

题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解答: 说实话这题目就是欺负中国人不了解罗马数字.可以参考维基百科中相关介绍:传送门.其中有性质如下: 羅馬數字共有7個,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 右加左減: 在較大的羅馬數字的右邊記上較小的羅馬數字,表示大數

LeetCode第[26]题(Java):Remove Duplicates from Sorted Array 标签:Array

题目难度:Easy 题目: Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extr

LeetCode第[3]题(Java):Longest Substring Without Repeating Characters 标签:Linked List

题目中文:没有重复字符的最长子串 题目难度:Medium 题目内容: Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is &q

LeetCode第[5]题(Java):Longest Palindromic Substring 标签:String、动态规划

题目中文:求最长回文子串 题目难度:Medium 题目内容: Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 翻译: 给定一个字符串s,找出s中最长的回文子串.你可以假设s的最大长度是1000. 什么叫回文子串? 就是字符串中,满足能正读反读都一样的子串,就是回文子串.如下所示 Input: "babad"

LeetCode第[14]题(Java): Longest Common Prefix

题目:最长公共前缀 难度:EASY 题目内容: Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 翻译:编写一个函数,在字符串数组中查找最长公共前缀字符串. 如果没有公共前缀,则返回空字符串. Example 1: Input: ["flow

LeetCode第[42]题(Java):Trapping Rain Water

题目:接雨水 难度:hard 题目内容: Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In