Leetcode [13] (Java): Roman to Integer

Source: Internet
Author: User

title : Roman Numeral conversion

problem difficulty : Easy

title Content : Roman numerals is represented by seven different symbols:,,,,, and I V X L C D .

Symbol       valuei             1V             5X             10L             50C             100D             500M             1000

For example, the written as in II Roman numeral, and the just, the added together. Twelve is written as, XII and which is simply X + II . The number twenty seven XXVII is written as and which is XX + V + II .

Roman numerals is usually written largest to smallest from left to right. However, the numeral for four are not IIII . Instead, the number four is written as IV . Because the one is before the five we subtract it making four. The same principle applies to the number nine, and which is written as IX . There is six instances where subtraction is used:

    • ICan be placed before V (5) and X (Ten) to make 4 and 9.
    • Xcan be placed before () and (+) to make and L C 90.
    • CCan be placed before D () and (+) to make and M 900.

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

translation :

For example, 2 is written in Roman numerals II, which is two added together. 12 is written, XII, which is x+ii. The number 27 is 27, which is xx+v+ii.

Roman numerals are most often written from left to right. However, the number of 4 is not IIII. Instead, the number 4 is written as IV, because 1 subtracts it by 5 before it equals 4. The same principle applies to number 9th, which is written in IX. There are six cases in which subtraction is used:

I can be placed before V (5) and X (10) respectively is 4 and 9.

X can be placed before L (50) and C (100), respectively, 40 and 90.

C can be placed before D (500) and M (1000), respectively, 400 and 900.

Given a Roman number, convert it to an integer. The input is within the range from 1 to 3999.

Ideas :

addition, subtraction matches only two characters before and after , so subtract is used when the cur character is less than the next character.

Use a map to store each character and corresponding value in order to compare and value

MyCode:

1      Public intRomantoint (String s) {2         if(s = =NULL||S.isempty ()) {3             return-1;4         }5         6         Char[] Schar =S.tochararray ();7Map<character, integer> map =NewHashmap<character, integer>();8Map.put (' I ', 1);9Map.put (' V ', 5);TenMap.put (' X ', 10); OneMap.put (' L ', 50); AMap.put (' C ', 100); -Map.put (' D ', 500); -Map.put (' M ', 1000); the          -         intSum=0; -          for(inti=0;i<schar.length-1;i++){ -             if(Map.get (Schar[i]) < Map.get (schar[i+1])) +Sum-=map.get (Schar[i]);//The current word transmitting the next small, then the sum minus this number -             Else +Sum+=map.get (Schar[i]);//Otherwise, directly add A         } at         returnSum+map.get (Schar[schar.length-1]);//because the last one is not judged, and there is no follow-up, so must be added -}

Result: Accept

Reference Answer :

1   Public intRomantoint (String s) {2     intnums[]=New int[S.length ()];3      for(intI=0;i<s.length (); i++){4         Switch(S.charat (i)) {5              CaseM:6nums[i]=1000;7                  Break;8              CaseD:9nums[i]=500;Ten                  Break; One              CaseC: Anums[i]=100; -                  Break; -              CaseL: theNums[i]=50; -                  Break; -              CaseX : -nums[i]=10; +                  Break; -              CaseV: +Nums[i]=5; A                  Break; at              Case' I ': -Nums[i]=1; -                  Break; -         } -     } -     intSum=0; in      for(inti=0;i<nums.length-1;i++){ -         if(nums[i]<nums[i+1]) tosum-=Nums[i]; +         Else -sum+=Nums[i]; the     } *     returnSum+nums[nums.length-1]; $}

answer idea : here directly to the original character array (string) into the corresponding int array, so that a map is not needed, relatively easy access to this method, but added a round of algorithm complexity, but the map access is more than a direct array to access a step, So the complexity of the two may be almost the same as O (2n), and the answer to this operation is more simple, can be used for reference.

Leetcode [13] (Java): Roman to Integer

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.