- 浏览: 183302 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given a roman numeral, convert it to an integer.
Input is guaranteed to be within the range from 1 to 3999.
我们做过Integer to Roman 这道题目,在里面讲了罗马数字的规则,这道题是罗马数字转整数,不明白规则的看一下上一道题目,代码如下:
Input is guaranteed to be within the range from 1 to 3999.
我们做过Integer to Roman 这道题目,在里面讲了罗马数字的规则,这道题是罗马数字转整数,不明白规则的看一下上一道题目,代码如下:
public class Solution { public int romanToInt(String s) { int result = 0; int next = 0; for(int i = s.length() - 1; i >= 0; i--) switch(s.charAt(i)) { case 'I': if(next > 1) result -= 1; else result += 1; next = 1; break; case 'V': if(next > 5) result -= 5; else result += 5; next = 5; break; case 'X': if(next > 10) result -= 10; else result += 10; next = 10; break; case 'L': if(next > 50) result -= 50; else result += 50; next = 50; break; case 'C': if(next > 100) result -= 100; else result += 100; next = 100; break; case 'D': if(next > 500) result -= 500; else result += 500; next = 500; break; case 'M': if(next > 1000) result -= 1000; else result += 1000; next = 1000; break; } return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 264Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 266You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 382Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 373Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 562Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 474Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 662Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 468The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 428Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 573Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 425All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 895Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 928Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 841Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 781You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
leetcode上Roman to Integer的完整C++代码,已被accepted
LeetCode Roman to Integer解决方案
13. 罗马数字转整数 Roman to Integer用哈希存储映射字符--->对映的值对字符串的字符挨个判断,考虑下一个字符如果下一个字符大于当前字符,su
13. Roman to Integer 15. 3Sum 16. 3Sum Closest 17. Letter Combinations of a Phone Number 18. 4Sum 19. Remove Nth Node From End of List 20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate ...
c c语言_leetcode 0013_roman_to_integer.zip
java入门 java_leetcode题解之013_Roman_to_Integer
6. **Integer to Roman.cpp** - 第40题,与“Roman to Integer”相反,这个任务是将整数转换为罗马数字。解冑需理解罗马数字的构造规则。 7. **Implement strStr().cpp** - 第28题,实现字符串查找功能,即在一个...
js js_leetcode题解之13-roman-to-integer.js
c语言入门 C语言_leetcode题解之13-roman-to-integer.c
* 整数到罗马(Integer to Roman):将整数转换为罗马数字。 * 罗马到整数(Roman to Integer):将罗马数字转换为整数。 4. 链表操作: * 删除链表中的重复项(Remove Duplicates from Sorted Array):删除链表...
12. **Integer to Roman** (Medium): 整数转换为罗马数字。需要对罗马数字系统有深入理解,并建立对应的转换规则。 13. **Roman to Integer** (Easy): 罗马数字转换为整数。通过建立罗马数字与数值的对应关系,逐...
12._INTEGER TO ROMAN_(整数转换为罗马数字) 题目描述:给定一个整数,将其转换为罗马数字。 知识点:数字处理、罗马数字 思路:使用罗马数字规则,将整数转换为罗马数字。 13._ROMAN TO INTEGER_(罗马数字转换...
Integer 8.String To Integer 9.Palindrome Number 10.String To Integer 11.Container With Most Water 12.Integer To Roman 13.Roman To Integer 289 347 380 442 457 Circular Array Loop 535 Encode and Decode ...
Integer 008 String to Integer (atoi) 009 Palindrome Number 010 Regular Expression Matching 011 Container With Most Water 012 Integer to Roman 013 Roman to Integer 014 Longest Common Prefix 015 3Sum ...
12. Integer to Roman:将整数转换为罗马数字。 13. Roman to Integer:将罗马数字转换为整数。 14. Longest Common Prefix:求多个字符串的最长公共前缀。 15. 3Sum:找到数组中所有和为0的不重复的三个数的组合...
Integer 简单 字串 String to Integer (atoi) 中等 字串 麻烦 Palindrome Number 简单 字串 Container With Most Water 中等 动态规划 重要 Integer to Roman 中等 重要 Roman to Integer 简单 重要 Longest Common ...
public class RomanToInteger { private static final Map, Integer> romanMap = new HashMap(); static { romanMap.put('I', 1); romanMap.put('V', 5); romanMap.put('X', 10); romanMap.put('L', 50); ...
36. Integer to Roman:将整数转换为罗马数字。 37. Roman to Integer:将罗马数字转换为整数。 38. Clone Graph:复制一个无向图。 39. Min Stack:设计一个栈,支持获取栈内最小元素的操作。 40. Evaluate Reverse...
Integer 008. String to Integer 009. Palindrome Number 010. Regular Expression Matching 011. Container With Most Water 012. Integer to Roman 013. Roman to Integer 014. Longest Common Prefix 019. R