Leetcode 13. Roman to Integer C language

Source: Internet
Author: User

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

The Roman numeral and the integer turn

Int getint (char c) {    int temp;    switch (c) {         case  ' I ':return 1;         case  ' V ':return 5;        case  ' X ': return  10;        case  ' L ':return 50;         case  ' C ': return 100;        case   ' D ':return 500;        case  ' M ':return 1000;         }        return 0 ;} Int romantoint (char* s)  {    int temp;    int  sum=getint (S[0]);     for (Int i=1;i<strlen (s); i++) {         if (getInt(S[i-1]) <getint (S[i]) {            sum+=getint ( S[i]);             sum-=2*getint (S[i-1]);             // printf ("%d-->%d\n", GetInt (S[i-1]) , GetInt (S[i]));                     }else{             Sum+=getint (S[i]);             // printf ("{%d} ", GetInt (S[i]));        }             }    return sum;}

PS: At first I thought it was a summation from left to right ... The results were found to be wrong. The first thing to do is to understand the Roman numeral rules ...... I don't know now. One by one, the left side is bigger than the right, and the left side is smaller than the right one, and then minus twice times more .....

2, for the first time in the Leetcode additional definition function, to write in the previous to the function .....

Leetcode 13. Roman to Integer C language

Related Article

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.