【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.



题解:转换的方法:从左往右扫描罗马字符,如果当前的字符对应的数字比上一个数字小,就直接加上这个数字;否则加上这个数字并且减去上一个数字的两倍,然后更新上一个数字。利用一个HashMap存放罗马字符和数字的对应。

罗马数字和阿拉伯数字的对应表格参见http://www.cnblogs.com/sunshineatnoon/p/3856057.html

例如罗马数字DCXIX:500+100+10+1+10-2 = 619

代码如下:

 1 public class Solution {
 2     public int romanToInt(String s) {
 3         if(s == null || s.length() == 0)
 4             return 0;
 5
 6         HashMap<Character, Integer> map= new HashMap<Character,Integer>();
 7         map.put(‘I‘, 1);
 8         map.put(‘V‘, 5);
 9         map.put(‘X‘, 10);
10         map.put(‘L‘, 50);
11         map.put(‘C‘, 100);
12         map.put(‘D‘, 500);
13         map.put(‘M‘, 1000);
14
15         int length = s.length();
16         int result = map.get(s.charAt(0));
17         int last = result;
18
19         for(int i = 1;i < length;i++){
20             int temp = map.get(s.charAt(i));
21             if(temp <= last)
22                 result += temp;
23             else
24                 result = result + temp - 2*last;
25             last = temp;
26         }
27
28         return result;
29     }
30 }

【leetcode刷题笔记】Roman to Integer

时间: 2025-01-17 14:17:20

【leetcode刷题笔记】Roman to Integer的相关文章

【leetcode刷题笔记】Integer to Roman

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 题解:基本的罗马字符和数字对应如下表所示: 罗马字符 数字 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 每隔一个字符又可以与相邻的两个字符组成一个新的数,例如I可以和V和X组成IV和IX,这样又可以生成6个数字,如下表: 罗马字符 数字 IV 4 IX

【leetcode刷题笔记】String to Integer (atoi)

Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Restore IP Addresses

Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135", return ["255.255.11.135", "255.255.111.35"]. (Order does not matter) 题解:深度优先搜索.用resul

【leetcode刷题笔记】Max Points on a Line

Given n points on a 2D plane, find the maximum number of points that lie on the same straight line. 题解: 思路比较简单,每条直线都可以表示为y=kx+b,所以对于任意三点,如果它们共线,那么它们中任意两点的斜率都相等. 所以就遍历points数组,对其中的每一个元素计算它和位于它后面的数组元素的斜率并保存在一个hashmap中. 这个hashmap的键就是两点构成直线的斜率,值就是和当前元素po

【leetcode刷题笔记】Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example,Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ] ] 题解:以前做过的Spiral Matrix是给一个矩阵螺旋式的输出,这道题是给一个n,螺旋式的

【leetcode刷题笔记】Substring with Concatenation of All Words

You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters. For example, given:S: "b

【leetcode刷题笔记】Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

【leetcode刷题笔记】4Sum

Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending order.