Question 1: Roman numerals to integers

Roman numerals contain the following seven characters: I, V, X, L, C, D and M.

Character value

I 1

V 5

X 10

L 50

C 100

D 500

M 1000

For example, the Roman numeral 2 is written as II, which is two parallel ones. 12 is written as XII, which is X + II. 27 is written as XXVII, which is XX + V + II.

Usually, the small Roman numerals are to the right of the large ones. But there are also special cases. For example, 4 is not written as IIII, but IV. The number 1 is on the left of the number 5, and the number represented is equal to the value 4 obtained by subtracting the decimal 1 from the large number 5. Similarly, the number 9 is represented as IX. This special rule applies only to the following six cases:

I can be placed to the left of V (5) and X (10) to represent 4 and 9.

X can be placed to the left of L (50) and C (100) to represent 40 and 90.

C can be placed to the left of D (500) and M (1000) to represent 400 and 900.

Given a Roman numeral, convert it to an integer.

Example 1:

Input: s = "III"

Output: 3

Example 2:

Input: s = "IV"

Output: 4

Example 3:

Input: s = "IX"

Output: 9

Example 4:

Input: s = "LVIII"

Output: 58

Explanation: L = 50, V= 5, III = 3

Example 5:

Input: s = "MCMXCIV"

Output: 1994

Explanation: M = 1000, CM = 900, XC = 90, IV = 4

Tips:

1 <= s.length <= 15

s contains only characters ('I ',' V ',' X ',' L ',' C ','D','M ')

The title data ensures that s is a valid Roman numeral and represents an integer in the range [1, 3999]

The test cases given in the title comply with the Roman numeral writing rules, and there will be no cross position and so on.

Examples such as IL and IM do not meet the title requirements. 49 should write XLIX and 999 should write CMXCIX.

For detailed writing rules of Roman numerals, please refer to Roman numerals - Mathematics.

Source: LeetCode

Link: https://leetcode-cn.com/problems/roman-to-integer

class Solution { public: int romanToInt(string s) { int sum=0; int i=0; while(i<s.size()){ if(s[i]=='I'){ if(s[i+1]=='V'){ sum+=4; i+=2; } else if(s[i+1]=='X'){ sum+=9; i+=2; } else{ sum+=1; i++; } } else if(s[i]=='V'){ sum+=5; i++; } else if(s[i]=='X'){ if(s[i+1]=='L'){ sum+=40; i+=2; } else if(s[i+1]=='C'){ sum+=90; i+=2; } else{ sum+=10; i++; } } else if(s[i]=='L'){ sum+=50; i++; } else if(s[i]=='C'){ if(s[i+1]=='D'){ sum+=400; i+=2; } else if(s[i+1]=='M'){ sum+=900; i+=2; } else{ sum+=100; i++; } } else if(s[i]=='D'){ sum+=500; i++; } else{ sum+=1000; i++; } } return sum; } };

Topic 2: longest common prefix

Write a function to find the longest common prefix in a string array.

Returns the empty string '' if there is no public prefix.

Example 1:

Input: strs = ["flower", "flow", "flight"]

Output: "fl"

Example 2:

Input: strs = ["dog", "racecar", "car"]

Output: ''

Explanation: the input does not have a public prefix.

Tips:

1 <= strs.length <= 200

0 <= strs[i].length <= 200

strs[i] consists of lowercase letters only

Source: LeetCode

Link: https://leetcode-cn.com/problems/longest-common-prefix

class Solution { public: string longestCommonPrefix(vector<string>& strs) { if(strs.size()==0) return ""; string ss=strs[0]; for(int i=1;i<strs.size();i++){ int index=0; int j=0; while(j<ss.size()&&j<strs[i].length()&&ss[j]==strs[i][j]){ index++; j++; } ss=ss.substr(0,index); } return ss; } };

Question 3: integer inversion

Give you a 32-bit signed integer x and return the result after reversing the number part in X.

If the inverted integer exceeds the range of 32-bit signed integers [− 231, 231 − 1], 0 is returned.

Assume that the environment does not allow the storage of 64 bit integers (signed or unsigned).

Example 1:

Input: x = 123

Output: 321

Example 2:

Input: x = -123

Output: - 321

Example 3:

Input: x = 120

Output: 21

Example 4:

Input: x = 0

Output: 0

Tips:

-231 <= x <= 231 - 1

Source: LeetCode

Link: https://leetcode-cn.com/problems/reverse-integer

class Solution { public: int reverse(int x) { if(x==0) return 0; long a=0; long sum=0; a=abs(x);//Absolute value of x while(a!=0){ int b=0; b=a%10; sum=sum*10+b; a=a/10; } if(sum>pow(2,31)) return 0;//pow(2,31) is to find the 31st power of 2 if(x<0) return -sum; return sum; } };