LeetCode题目记录
用于记录LeetCode的题目、分析和答案。
4. Median of Two Sorted Arrays
描述
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:
1
2
3
4 nums1 = [1, 3]
nums2 = [2]
The median is 2.0Example 2:
1
2
3
4 nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
分析
参考LeetCode给出的解答
题目要求寻找两个有序数组合并后的中位数且要求时间复杂度为,即需要采用二分法进行查找。
中位数即处在一个顺序数列中最中间的一个数(或两个数的平均值),因此,若将两个数列合二为一并进行排序形成num3,则:
设i为num1的索引,j为num2的索引,则
且
对于,,,这种临界情况,在本题中表示已经获得了合适的,可直接开始计算中位数。
当为奇数时,中位数为
,若其中一个不存在则直接为另一个;
当为奇数时,中位数为
,对和,若其中一个不存在则函数值直接为另一个。
实现
参考LeetCode给出的解答
代码
1 | class Solution { |
复杂度分析
-
时间复杂度:
在该算法中,对两字符串中短的那一个进行了二分法查找,因此时间复杂度为。
-
空间复杂度:
在该算法中使用了9个变量(
m, n, temp1, temp2, iMax, iMin, half, maxLeft, minRight)来存储数据。
5. Longest Palindromic Substring
描述
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
1
2
3 Input: "babad"
Output: "bab"
Note: "aba" is also a valid answer.Example 2:
1
2 Input: "cbbd"
Output: "bb"
分析
题目要求在给出的字符串中寻找最长回文子字符串,可采用以下几种方法:
寻找最大公共子串
可使用将原始字符串倒置形成新字符串并求取两字符串的最长公共子串的方式得到结果,但是需要注意检查字串在两字符串中的索引,以防出现输入aacdefcaa最终的出的结果为acc的情况发生。寻找最长公共子串可使用动态规划。假设该字符串长为,原字符串为,倒置后的字符串为,则创建一个的二维数组,令分别为两字符串的索引(从到表示字符串索引),计算过程中满足以下公式:
围绕中心扩张
回文字符串的特点为中心对称,即对于一个回文字符串来说,其沿中心对称的两个字符一定相同。对于一个长为的字符串来说,其可能的对称中心为个(每一个字符以及每两个字符的中间都可能是对称中心)。因此可以对这个可能的中心沿中心向两边扩张,记录下最长的回文字符串即可。
Manacher’s Algorithm
参考 Manacher’s Algorithm 马拉车算法
实现
寻找最大公共字串
代码
1 | class Solution { |
复杂度分析
-
时间复杂度:
在该算法中对创建的的二维数组进行了遍历。
-
空间复杂度:
该算法创建了大小为的二维数组用来存储寻找最大公共字串时用到的数据。
围绕中心扩张
代码
1 | class Solution { |
复杂度分析
-
时间复杂度:
对于
expandFromCenter函数,其循环最多可能执行次。 -
空间复杂度:
Manacher’s Algorithm
代码
1 | class Solution { |
复杂度分析
- 时间复杂度:
- 空间复杂度:
6. ZigZag Conversion
描述
The string
"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
1
2
3 P A H N
A P L S I I G
Y I RAnd then read line by line:
"PAHNAPLSIIGYIR"Write the code that will take a string and make this conversion given a number of rows:
1 string convert(string s, int numRows);Example 1:
1
2 Input: s = "PAYPALISHIRING", numRows = 3
Output: "PAHNAPLSIIGYIR"Example 2:
1
2
3
4
5
6
7
8 Input: s = "PAYPALISHIRING", numRows = 4
Output: "PINALSIGYAHRPI"
Explanation:
P I N
A L S I G
Y A H R
P I
分析
题目要求将给出的字符串排列成规定行数的“Z”字型(从上到下,再到右上),并输出按照从左到右、从上到下的正常顺序重新组合的字符串。
对“Z”字形排列进行观察可以发现,字符排列是有周期的,每个周期中第一行和最后一行只有一个字符,其它行则有两个。每个周期中每一行的第一个字符索引为:行数+单周期长度*目前已经过的周期数,而对于有第二个字符的行来说,第二个字符的索引为:单周期长度*(目前已经过的周期数+1)-行数。
实现
代码
来自LeetCode
1 | class Solution { |
复杂度分析
-
时间复杂度:
字符串中每一个字符都只访问了一次。
-
空间复杂度:
创建了一个长度与原字符串长度相同的字符串来存储转换后的字符串。
7. Reverse Integer
描述
Given a 32-bit signed integer, reverse digits of an integer.
Example 1:
1
2 >Input: 123
>Output: 321Example 2:
1
2 >Input: -123
>Output: -321Example 3:
1
2 >Input: 120
>Output: 21Note:
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.
分析
题目要求将输入的整数逆序后输出并保留正负号,且当处理后的数超过了32位int的范围时返回0。
得到逆序的数可使用不断对原数取除以10的余数并依次乘10相加即可得到逆转后的数。由于除法和取余会保留负号,因此无需对负号进行特殊处理。要验证得出的结果是否在32位int范围内(即[2147483647, -2147483648]),可验证目前已经得到的数(还未乘10并加上目前得到的余数前的数)是否小于MAX_INT / 10或者大于MIN_INT / 10,满足则未溢出;另外,若已经得到的数等于MAX_INT / 10,则需验证当前得到的余数是否大于7,若大于7则溢出,同时还需检验已经得到的数等于MIN_INT / 10,则需检验目前得到的余数是否小于-8,若小于-8则溢出。
实现
代码
1 | class Solution { |
复杂度分析
-
时间复杂度:
中有 或个数字
-
空间复杂度:
8. String to Integer (atoi)
描述
Implement
atoiwhich converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
- Only the space character
' 'is considered as whitespace character.- Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
Example 1:
1
2 Input: "42"
Output: 42Example 2:
1
2
3
4 Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.Example 3:
1
2
3 Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.Example 4:
1
2
3
4 Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.Example 5:
1
2
3
4 Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
分析
题目给出一个字符串,要求获取其第一个不为空格的字符,若该字符为正号、负号或者数字,则获取其之后若干个数字,遇到其它字符则停止,最后将获取到的数字组合成int 类型的整数返回;若第一个非空格字符不是前面说的三种字符或该字符串为空或该字符串只包含空格,则直接返回0。另外,若组合成的数字超过了int类型的范围,则输出int类型的最大值或最小值(取决于获取的实际数字的正负)。
本题总的来说就是一堆if-else的堆砌(这也许就是该题在LeetCode上被骂的原因吧)。分析在下面代码的注释中。
实现
代码
1 | class Solution { |
复杂度分析
- 时间复杂度:
- 空间复杂度:
9. Palindrome Number
描述
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.
Example 1:
1
2 Input: 121
Output: trueExample 2:
1
2
3 Input: -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:
1
2
3 Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.Follow up:
Coud you solve it without converting the integer to a string?
分析
题目给出一个数字,要求判断该数字正读和反读是否相同。
可将该数字转换为字符串,判断逆转后的字符串和原字符串是否相同即可;也可以不借助字符串,将原数每位取余并逐位添加到新数上,最后比较两数是否相同。
实现
代码
1 | class Solution { |
复杂度分析
- 时间复杂度:
- 空间复杂度:
10. Regular Expression Matching
描述
Given an input string (
s) and a pattern (p), implement regular expression matching with support for'.'and'*'.
1
2 '.' Matches any single character.
'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).
Note:
scould be empty and contains only lowercase lettersa-z.pcould be empty and contains only lowercase lettersa-z, and characters like.or*.Example 1:
1
2
3
4
5 Input:
s = "aa"
p = "a"
Output: false
Explanation: "a" does not match the entire string "aa".Example 2:
1
2
3
4
5 Input:
s = "aa"
p = "a*"
Output: true
Explanation: '*' means zero or more of the preceding element, 'a'. Therefore, by repeating 'a' once, it becomes "aa".Example 3:
1
2
3
4
5 Input:
s = "ab"
p = ".*"
Output: true
Explanation: ".*" means "zero or more (*) of any character (.)".Example 4:
1
2
3
4
5 Input:
s = "aab"
p = "c*a*b"
Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore, it matches "aab".Example 5:
1
2
3
4 Input:
s = "mississippi"
p = "mis*is*p*."
Output: false
分析
题目要求实现简单的正则匹配,只需支持.和*。
可采用递归或者动态规划。
实现
递归
代码
1 | class Solution { |
复杂度分析
- 时间复杂度: