"Leetcode from zero single brush" Roman to Integer

Source: Internet
Author: User

Topic:

Given a Roman numeral, convert it to an integer.

Input is guaranteed to being within the range from 1 to 3999.

Answer:

To be honest, the problem is bullying Chinese people don't know Roman numerals. Refer to the relevant Wikipedia: Portal. Among them are the following properties:

  • There are 7 Roman numerals, namely I (1), V (5), X (10), L (50), C (100), D (500), and M (1000).
  • Right plus left minus: a smaller number of Roman numerals on the right side of the larger Roman numerals, indicating large numbers plus small numbers. The smaller Roman numerals on the left side of the larger Roman numerals indicate that the large numbers decrease the number of words.
  • The next thing is to check the table, and do not consider the problem of the system, a count of one.

    Compares the size of the letter to the right, and if it is greater than or equal to the right letter, the sum is added to the letter;

    Class Solution {public:    int Romantoint (string s) {        Std::map<char, int>table;        table[' I '] = 1;        table[' V '] = 5;        table[' X '] = ten;        table[' L '] =;        table[' C '] = +;        table[' D '] =;        table[' M '] = +;                int len = S.size ();        int ans = 0;        for (int i = 0; i<len-1; i++)        {            if (Table[s[i]] < table[s[i+1]])                ans = ans-table[s[i]];            else                ans = ans + table[s[i]];        }        Ans + = table[s[len-1]];                return ans;    };

    "Leetcode from zero single brush" Roman to Integer

    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.