高级农民
- 积分
- 1187
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-9-6
- 最后登录
- 1970-1-1
|
第三天打卡, 主要做了string里面的sliding window 和 panlindrome 类型的题目。
| # | 名称 | Tag& 频率 | 题目 | 解题 | | 76 | Minimum Window Substring | Substring
Sliding Window | [color=rgba(0, 0, 0, 0.65)]Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). [color=rgba(0, 0, 0, 0.65)]Example: Input: S = "ADOBECODEBANC", T = "ABC"Output: "BANC"
| 窗口类问题,这类问题解体思路和步骤一般如下:
1. 首先我们需要一个map来存t里面所有字符和其对应的出现次数,和一个count变量,
其值等于t的长度来记录还有多少个字母需要匹配,每匹配上一个count--,
当为0的时候说明我们找到一个窗口
2. 我们需要2个变量来追踪当前最小窗口的起始位置和长度
3. 遍历s,过程中我们检查当前字符在map中的次数,大于0则把他对应的count--,然后总count--。
当总count等于0,我们找到一个窗口,更新最小窗口起始位置和长度
4. 最后还要检测下当整个字符串为一个合法子串作为结果和整个字符串不合法返回空串的情况。
| | 30 | Substring with Concatenation of All Words | SubstringSliding Window
| [color=rgba(0, 0, 0, 0.65)]You are given a string, s, and a list of words, words, that are all of the same lengths.
Find all starting indices of substring(s) in s that is a concatenation of each word in
words exactly once and without any intervening characters.
Example 1: Input: s = "barfoothefoobarman", words = ["foo","bar"]Output: [0,9]Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively. | 这题有多种解法,我们还是来看窗口解法,
需要从s里面找到包含了words里面所有单词的窗口。
题目告诉我们单词的长度都一样,所以我们可以算出窗口的长度。然后一步步的移动窗口。
和上题一样,我们需要一个map来存当前word出现的次数,然后在每一个窗口,
我们需要一个个截取word长度的子串到map中判定是否在word中出现过。
这里,在每个窗口我们需要创建另外一个临时的map,
来记录这个窗口里words里面单词出现的次数,
最后当窗口比对完了之后我们只需要看两个map是否相等即可。 | | 3 | Longest Substring Without Repeating Characters | SubstringSliding Window
| Given a string, find the length of the[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]longest substring[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]without repeating characters.[color=rgba(0, 0, 0, 0.65)] Example 1: Input: "abcabcbb"Output: 3 Explanation: The answer is "abc", with the length of 3.
| 依然是窗口解法,这个不需要计数但是不能重复,还是要一个map来记录字母上一次出现的位置。
然后遍历过程我们维护一个窗口,我们把出现的字母的index加入/更新到map里面。
当遇到字母已经在map里面,说明遇到重复,需要跟新左边界:
我们就取该字符上一次出现位置+1和窗口左边界left两个中的大值。例子:
- abba, 第二个b出现时候,b上一次位置+1比left大,窗口left为第二个b
- abba,第二个a出现的时候,a上一次位置+1比left小,窗口left还是为left
最后一步我们在用窗口大小来跟新最大长度就好
| | 125 | Valid Palindrome | Palindrometwo-pointer
| Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. [color=rgba(0, 0, 0, 0.65)]Note: For the purpose of this problem, we define empty string as valid palindrome. [color=rgba(0, 0, 0, 0.65)]Example 1: Input: "A man, a plan, a canal: Panama"Output: true | two pointer解法,由于只需要验证字母数字,所以遇到不是字母数字的直接跳过,
直到左右指针都是字母数字,我们就比较是否相等,
不等直接返回false,相等移动指针继续比较。 | | 5 | Longest Palindromic Substring | Palindrome | Given a string[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]s[color=rgba(0, 0, 0, 0.65)], find the longest palindromic substring in[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]s[color=rgba(0, 0, 0, 0.65)]. You may assume that the maximum length of[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]s[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]is 1000. [color=rgba(0, 0, 0, 0.65)]Example 1: Input: "babad"Output: "bab"Note: "aba" is also a valid answer. | 遍历字符,从当前字符往两边扩展直到不相等或者到达边界,
然后跟新比较当前的palindromic substring到结果 | | 9 | Palindrome Number | Palindrome | Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. [color=rgba(0, 0, 0, 0.65)]Example 1: Input: 121Output: true | 这题其实不算字符串到题了,验证数字是否回文。
最容易想到到做法就是把数拆成一个个数字装进数组,然后在验证。
但是也可以不开数组做,做法就是找到数字最大divisor,比如1221,divisor就是1000,
然后我们可以用num / divisor找到第一位,num % 10找到最后一位来进行比较 | | 214 | Shortest Palindrome | Palindrome | Given a string[color=rgba(0, 0, 0, 0.65)] s[color=rgba(0, 0, 0, 0.65)], you are allowed to convert it to a palindrome by adding characters in front of it. Find and return the shortest palindrome you can find by performing this transformation. [color=rgba(0, 0, 0, 0.65)]Example 1: Input: "aacecaaa"Output: "aaacecaaa" | 要求从字符串开头加字符,直到整个字符串成为palindrome。
思路很简单,把字符反转存到revt,然后从revt从0开始取子串,
直到子串等于s的开始子串。input: cacbb
revt = bbcac
当i=2,子串cac == input.startswith(cac), 然后截取revt0到i到子串加到s, 子串为bb
| | 336 | Palindrome Pairs | Palindrome | Given a list of[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]unique[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]words, find all pairs of[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]distinct[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]indices[color=rgba(0, 0, 0, 0.65)] (i, j)[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]in the given list, so that the concatenation of the two words, i.e.[color=rgba(0, 0, 0, 0.65)] words + words[j][color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]is a palindrome. [color=rgba(0, 0, 0, 0.65)]Example 1: [color=rgba(0, 0, 0, 0.65)]Input: ["abcd","dcba","lls","s","sssll"]Output: [[0,1],[1,0],[3,2],[2,4]] Explanation: The palindromes are ["dcbaabcd","abcddcba","slls","llssssll"]
| 这道题的思路就是每个字符串,我们可以把它划分prefix和sufix,
比如例子里面到abcd,我们prefix可以划分[‘’, ‘a’, ‘ab’, ‘abc’, ‘abcd’], 对应sufix为[‘abcd’, ‘bdc’, ‘cd’, ‘d’, ‘’]。
当prefix为palindrome的时候,
我们只需要看对应sufix反转之后的字符是否在word list里面,
如果有,就找到一个pair。
这里prefix为’”’时候,sufix为’abcd’, 反转’dcba’,
然后我们在word list里面找到’dcba’, 把他们两个结果加入最终结果,然后继续搜索。记得也要查反转prefix能找到word的case | | 131 | Palindrome Partitioning | Palindrome | Given a string[color=rgba(0, 0, 0, 0.65)] [i]s[color=rgba(0, 0, 0, 0.65)], partition[color=rgba(0, 0, 0, 0.65)] s[color=rgba(0, 0, 0, 0.65)] [color=rgba(0, 0, 0, 0.65)]such that every substring of the partition is a palindrome. [color=rgba(0, 0, 0, 0.65)]Return all possible palindrome partitioning of s. [color=rgba(0, 0, 0, 0.65)]Example: Input: "aab"Output:[ ["aa","b"], ["a","a","b"]] | 要求找出所有划分字符串且所有子串都为回文串的方法,
直接用了DFS解决,枚举所以子串,检测是否为回文串,
是的话加入subset继续搜索。好像还有DP解法,下次解题再来做。 | |
| | | |
|
|