Leetcode Roman to Integer Roman character to digital problem solving report

Source: Internet
Author: User

https://oj.leetcode.com/problems/roman-to-integer/

Given a Roman numeral, convert it to an integer.
Input is guaranteed to being within the range from 1 to 3999.

Convert a given Roman character to a number. The first thing to understand is the rules that Roman characters represent.
First, there are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).
Second, on the right side of the larger Roman numerals, a smaller number of Roman numerals, indicating large numbers plus a small number, for example: VI (6), VIII (8), LV (+ 5), LX (50 + 10)
On the left side of the larger Roman numerals, a smaller number of Roman numerals, indicating a large number of decimal words, for example: IX (9), XL (50-40), XC (100-10)
Four, left minus the number is limited, only I, X, C. For example, 45 can not be written as VL, only XLV.
Five, the left minus time cannot cross a digit. For example, 99 may not be represented by an IC (100-1), but rather by XCIX ([100-10] + [10-1]).

The way of thinking, there are two processing logic:
one, to determine whether the current character is smaller than the next character, if small, you need to use the next character minus the current word multibyte to the final result character.
two, otherwise, the current character is added directly to the final result character.

The following is the AC code:

public class Solution {hashmap<character, integer> romantointmap = new Hashmap<character, integer> () {put (' I ', new Integer (1));p ut (' V ', new Integer (5));p ut (' X ', new integer);p ut (' L ', new integer);p ut (' C ', new Integer ( );p ut (' D ', new integer);p ut (' M ', new Integer (1000));}}; public int Romantoint (String s) {int ret = 0;int i = 0;while (I<s.length ()) {if (S.charat (i) = = ' I ' | | S.charat (i) = = ' X ' | | S.charat (i) = = ' C ') {if ((i+1) <= (S.length ()-1) &&romantointmap.get (S.charat (i)) <romantointmap.get ( S.charat (i+1)) {ret + romantointmap.get (S.charat (i+1))-Romantointmap.get (S.charat (i)); i = i + 2;continue;}} RET + = Romantointmap.get (S.charat (i)); i++;} return ret;}}




Leetcode Roman to Integer Roman character to digital problem solving report

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.