[Leetcode] 14. Longest Common Prefix

14. Longest Common Prefix Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: String 一、題目 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: strs = [“flower”, “flow”, “flight”] Output: “fl” Example 2: Input: strs = [“dog”, “racecar”, “car”] Output: "" Explanation: There is no common prefix among the input strings. Constraints: 1 <= strs.length <= 200 0 <= strs[i].length <= 200 strs[i] consists of only lowercase English letters. 二、分析 簡單的字串比對問題。 需熟悉 string 的函數 substr() 的使用方式,常用以下兩種 s.substr(int start, int len),從 start 起取長度為 len 的子字串。 s.substr(int start) 從 start 起取到字串的結尾。 三、解題 1. String Time complexity: \(O(m\times n),\text{m }為\text{ strs }的長度,\text{n }為\text{ strs[i] }的長度\), Space complexity: \(O(1)\) string longestCommonPrefix(vector<string>& strs) { string res = strs[0]; for (int i = 1; i < strs.size(); i++) { int j = 0; for (; j < min(strs[i].length(), res.length()); j++) { if (strs[i][j] != res[j]) break; } res = res.substr(0, j); } return res; } 回目錄 Catalog ...

<span title='2022-10-28 00:00:08 +0800 +0800'>October 28, 2022</span>&nbsp;·&nbsp;1 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 13. Roman to Integer

13. Roman to Integer Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Hash Table、Math、String 一、題目 Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. \(\boxed{\begin{array}{ll} \textbf{Symbol}&\textbf{Value}\\ \texttt{I}&1\\ \texttt{V}&5\\ \texttt{X}&10\\ \texttt{L}&50\\ \texttt{C}&100\\ \texttt{D}&500\\ \texttt{M}&1000\\ \end{array}}\) For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 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: ...

<span title='2022-10-27 21:58:08 +0800 +0800'>October 27, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 835. Image Overlap

835. Image Overlap Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Matrix 一、題目 You are given two images, img1 and img2, represented as binary, square matrices of size n x n. A binary matrix has only 0s and 1s as values. We translate one image however we choose by sliding all the 1 bits left, right, up, and/or down any number of units. We then place it on top of the other image. We can then calculate the overlap by counting the number of positions that have a 1 in both images. Note also that a translation does not include any kind of rotation. Any 1 bits that are translated outside of the matrix bordered are erased. Return the largest possible overlap. ...

<span title='2022-10-27 17:35:25 +0800 +0800'>October 27, 2022</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 12. Integer to Roman

12. Integer to Roman Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Hash Table、Math、String 一、題目 Roman numerals are represented by seven different symbols: I, V, X, L, C, D, and M. \(\boxed{\begin{array}{ll} \textbf{Symbol}&\textbf{Value}\\ \texttt{I}&1\\ \texttt{V}&5\\ \texttt{X}&10\\ \texttt{L}&50\\ \texttt{C}&100\\ \texttt{D}&500\\ \texttt{M}&1000\\ \end{array}}\) For example, 2 is written as II in Roman numeral, just two one’s added together. 12 is written as XII, which is simply X + II. The number 27 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: ...

<span title='2022-10-26 20:36:08 +0800 +0800'>October 26, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 11. Container With Most Water

11. Container With Most Water Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Array、Two Pointer、Greedy 一、題目 You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]). Find two lines that together with the x-axis form a container, such that the container contains the most water. Return the maximum amount of water a container can store. Notive that you may not slant the container. ...

<span title='2022-10-26 19:36:06 +0800 +0800'>October 26, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 10. Regular Expression Matching

10. Regular Expression Matching Hardness: \(\color{red}\textsf{Hard}\) Ralated Topics: String、Dynamic Programming、Recursion 一、題目 Given an input string s and a pattern p, implement regular expression matching with support for '.' and '*' where: '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). Example 1: Input: s = “aa”, p = “a” Output: false Explanation: “a” does not match the entire string “aa”. Example 2: ...

<span title='2022-10-26 18:36:01 +0800 +0800'>October 26, 2022</span>&nbsp;·&nbsp;3 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 9. Palindrome Number

9. Palindrome Number Hardness: \(\color{green}\textsf{Easy}\) Ralated Topics: Math 一、題目 Given an integer x, return true if x is palindrome number. An integer is a palindrome when it reads the same backward as forward. For example, 121 is a palindrome while 123 is not. Example 1: Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left. Example 2: Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome. Example 3: ...

<span title='2022-10-26 10:20:08 +0800 +0800'>October 26, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 8. String to Integer (atoi)

8. String to Integer (atoi) Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String 一、題目 Implement the myAtoi(string s) function, which converts a string to a 32-bit signed integer (similar to C/C++’s atoi function). The algorithm for myAtoi(string s) is as follows: Read in and ignore any leading whitespace. Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present. Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored. Convert these digits into an integer (i.e. "123" -> 123, "0032" -> 32). If no digits were read, then the integer is 0. Change the sign as necessary (from step 2). If the integer is out of the 32-bit signed integer range [-2^31, 2^31-1], then clamp the integer so that it remains in the range. Specifically, integers less than -2^31 should be clamped to -2^31, and integer greater than 2^31-1 should be clamped to 2^31-1. Return the integer as the final result. Note: ...

<span title='2022-10-26 00:21:56 +0800 +0800'>October 26, 2022</span>&nbsp;·&nbsp;4 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 7. Reverse Integer

7. Reverse Integer Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: Math 一、題目 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-2^31, 2^31-1], then return 0. Assume the environment does not allow you to store 64-bit integers (signed or unsigned). Example 1: Input: x = 123 Output: 321 Example 2: Input: x = -123 Output: -321 Example 3: ...

<span title='2022-10-25 23:47:53 +0800 +0800'>October 25, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu

[Leetcode] 6. Zigzag Conversion

6. Zigzag Conversion Hardness: \(\color{orange}\textsf{Medium}\) Ralated Topics: String 一、題目 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to dispaly this pattern in a fixed font for better legibility) \( \quad\texttt{P A H N}\\ \quad\texttt{APLSIIG}\\ \quad\texttt{Y I R}\\ \) And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string s, int numRows ...

<span title='2022-10-25 21:40:24 +0800 +0800'>October 25, 2022</span>&nbsp;·&nbsp;2 min&nbsp;·&nbsp;Rain Hu