Leetcode 13 14

94 阅读2分钟

日常划水~

13 Roman to Integer

题目描述:

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

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

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

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is 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, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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

Example 1:

Input: "III"
Output: 3

Example 2:

Input: "IV"
Output: 4

Example 3:

Input: "IX"
Output: 9

Example 4:

Input: "LVIII"
Output: 58
Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: "MCMXCIV"
Output: 1994
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

提示:

I - 1
V - 5
X - 10
L - 50
C - 100
D - 500
M - 1000

思路:题目要求将输入的罗马数字转换为对应的阿拉伯数字,并且遵循某些规则

Rules:

  • If I comes before V or X, subtract 1 eg: IV = 4 and IX = 9
  • If X comes before L or C, subtract 10 eg: XL = 40 and XC = 90
  • If C comes before D or M, subtract 100 eg: CD = 400 and CM = 900

对于两种元素之间的映射,自然想起使用Hash map建立罗马数字和对应值之间的映射关系,这里使用unorder_map

AC 代码:

class Solution {
public:
    int romanToInt(string s) {
        int r = 0;
        //使用map建立罗马数字和对应值之间的hash map,并初始化
        unordered_map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};
        //遍历输入的字符串
        for (int i = 0; i < s.size(); ++i) {
            int v = m[s[i]];
            //如果当前是最后一个字符,或是它大于后面的字符,直接将其加到结果中
            if (i == s.size() - 1 || m[s[i]] >= m[s[i+1]]) 
                r += v;
            //否则说明当前的字符小于后面的字符,将从结果中减去相应的值
            else 
                r -= v;
        }
        return r;
    }
};

14 Longest Common Prefix

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

If there is no common prefix, return an empty string "".

Example 1:

Input: ["flower","flow","flight"]
Output: "fl"

Example 2:

Input: ["dog","racecar","car"]
Output: ""
Explanation: There is no common prefix among the input strings.

Note:

All given inputs are in lowercase letters a-z.

AC代码:

class Solution {
public:
    //寻找字符串数组中最长的公共子字符串
    string longestCommonPrefix(vector<string>& strs) {
        //若字符串为空,返回""
        if (strs.empty()) return "";
        string res = "";
        //strs[0] = "flower"
        //j 用来遍历字符集中的字符串
        for (int j = 0; j < strs[0].size(); ++j) {
            char c = strs[0][j];
            //i 用来遍历字符串中的字符
            for (int i = 1; i < strs.size(); ++i) {
                //若当前的子字符串的长度大于相比较的字符串或是当前字符和比较的不同,返回结果
                if (j >= strs[i].size() || strs[i][j] != c) {
                    return res;
                }
            }
            //字符相同,则加到结果中
            res.push_back(c);
        }
        return res;
    }
};