Leetcode12~14 Integer to Roman/roman to Integer/longest Common Prefix

Source: Internet
Author: User

One: Integer to Roman

Topic:

Given an integer, convert it to a Roman numeral.

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

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

Analysis: The key is to determine how the Roman numerals, such as 4 is not IIII but iv,9 is not VIIII but IX, through the constant removal of the highest and the rest of the results obtained

Code:

Class Solution {public:    ///Key is 4 9 400 and other representations of different    string inttoroman (int num) {        string roman[13] ={"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};  Determine the correspondence between each other        int arabic[13] = {1000,900,500,400,100,90,50,40,10,9,5,4,1};        string result;        for (int i = 0; i < i++) {            int fac = num/arabic[i];   Remove the highest bit, not according to decimal/10 for            (int j = 0; J < FAC; J + +) {                result + = Roman[i];            }            num = num% arabic[i];   The remaining        }        return result;}            ;
Two:

Roman to Integer

Topic:

Given a Roman numeral, convert it to an integer.

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

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

Analysis: This is more simple, as long as the analysis of the current string character bit and the subsequent size of the relationship between the judgment is 4,9 and so on such a special case to be judged.

Class Solution {public:    solution () {        string Roman ={"MDCLXVI"};        int arabic[7] = {1000,500,100,50,10,5,1};        for (int i = 0; i < 7; i++) {            Hmap.insert (pair<char,int> (Roman[i], arabic[i]));    Determine the correspondence between the two        }    }    int Romantoint (string s) {       int result = 0;       int i = 0;       if (s.size () = = 0) return 0;       if (s.size () = = 1) return hmap[s[0]];       while (I < s.size ()) {            int x1 = hmap[s[i]];            int x2 = hmap[s[i+1]];            if (x1 >= x2) {                   //greater than normal                result + = x1;                i = i+1;            }            else {                       //less than abnormal description is 4 9 400 900 etc. these numbers                result + = x2-x1;                i = i+2;            }        }        return result;    } Private:    Map<char, int> hmap;};

three: Longest Common Prefix

Topic:

Write a function to find the longest common prefix string amongst an array of strings.

Links: https://leetcode.com/problems/longest-common-prefix/

Analysis: The problem is to seek the longest public prefix, this can be used hash_set, with all the prefix of the first string to establish a set, and then for each of the following string, see if his prefix in Hash_set, not to judge whether it is smaller than the previous Precommon, small to take the shortest

Code:

Class Solution {public:    string Longestcommonprefix (vector<string> &strs) {        if (strs.size () <= 0) Return "";        Set<string> Hset;        String str0 = Strs[0];        int Precommon =  str0.size ();        for (int i = 0; i < str0.size (); i++) {  //The prefix of the first string establishes a hash_set            Hset.insert (Str0.substr (0,i+1));        }        for (int i = 1; i < strs.size (); i++) {            string str = strs[i];             Precommon = Min (Precommon, (int) str.size ());   The public prefix must be the shortest             if (Precommon = = 0) break in the string;            for (int j = 0; J < Str.size (); j + +) {                if (!hset.count (Str.substr (0,j+1))) {         //determine if the prefix for the preceding string O (1) time is done                   Precommon = Min (Precommon, j);      The shortest break is not present in the Hash_set                   ;        }}} Return str0.substr (0, Precommon);}    ;



Leetcode12~14 Integer to Roman/roman to Integer/longest Common Prefix

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.