[Microsoft 2023.2.27 Suzhou Internship One-Writing Test Questions] Converting Roman Numerals to Integers

[Software Special Forces 2023.2.20 written test questions] Minimum number of indentation operations

All the topics in this series are the feedback from the group friends in the interview practice group after the interview. Students who want to join the group to exchange and share fresh Internet interview experience, please follow my WeChat public account: "Running Luban No. 7 " , Reply: "Interview learning and exchange group" Obtain the QR code of the group, and just join the group to learn and share .

[Microsoft 2023.2.27 Practice Questions] Converting Roman Numerals to Integers
is an original leetcode question. Converting Roman Numerals to Integers is a simple question.

topic description

insert image description here
insert image description here
insert image description here

problem solving ideas

  Simulate the calculation process of the final result. It is to traverse from left to right to get each Roman numeral, and then add the corresponding Roman numeral to the final result. Note that six special cases are given in the title here. For these six special cases, they can also be regarded as the same as ordinary Roman numerals, which are mapped from Roman numerals to numbers and accumulated.
  So our calculation method is as follows:

  1. iterate over the string
  2. Determine whether the current index position is the end of the string, if not, determine whether the string composed of the Roman numeral and the Roman numeral behind it is a Roman combination of special rules, and if so, add the integer corresponding to the Roman numerals of the special rule, The index moves backward two places.
  3. Condition 2 is not met (the current index is the last position, or the combination of the current index and a Roman numeral after the index does not meet the special rules), then directly find the integer corresponding to the Roman character, add them up, and move the index backward by one .
  4. Traverse to the end of the string and return the accumulated value.
class Solution {
    
    
public:
    int romanToInt(string s) {
    
     
        map<string,int> roman2Int = {
    
    {
    
    "I",1},{
    
    "V",5},{
    
    "X",10},{
    
    "L",50},{
    
    "CD",400},
                                    {
    
    "C",100},{
    
    "D",500},{
    
    "M",1000},{
    
    "CM",900},
                                    {
    
    "IV",4},{
    
    "IX",9},{
    
    "XL",40},{
    
    "XC",90}};
        int rst = 0;
        //1.遍历字符串
        for(int i = 0; i<s.size(); i++){
    
    
            //2.1 判断该索引是否是字符串末尾
            if(i+1<s.size()){
    
    
                string specialCombination = s.substr(i,2);
                //2.2 判断该索引位置的罗马数和它后一位的罗马数的组合是否满足特殊字符的要求
                if(roman2Int.find(specialCombination) != roman2Int.end()){
    
    
                   //2 满足特殊字符的要求,按照特殊字符处理,索引向后移动两位。
                    rst += roman2Int[specialCombination];
                    i++;
                    continue;
                }         
            }
            //3 条件2没有满足,那么直接找到该罗马字符对应的整数,累加起来,索引向后移动一位。
            rst += roman2Int[s.substr(i,1)];
        }
        //4. 遍历到字符串结尾,返回累加值即可。
        return rst;
    }
};

Supongo que te gusta

Origin blog.csdn.net/weixin_41937380/article/details/129252411
Recomendado
Clasificación