class Solution: def detectCapitalUse(self, word: str) -> bool: if len(word) <= 1: return True if ord(word[0]) < 95 and ord(word[1]) < 95: for i in range(2, len(word)): if ord(word[i]) > 95: return False else: for i in range(1, len(word)): if ord(word[i]) < 95: return False return True 思路2
class Solution: def validPalindrome(self, s: str) -> bool: def isPalidrome(s: str): l = 0 r = len(s)-1 while l < r: if s[l] != s[r]: return False else: l += 1 r -= 1 return True
left = 0 right = len(s) - 1 while left < right: if s[left] != s[right]: return isPalidrome(s[left:right]) or isPalidrome(s[left + 1:right + 1]) else: left += 1 right -= 1 return True 12. 整數轉羅馬數字
難度:中等
羅馬數字包含以下七種字符:I, V, X, L,C,D 和 M。
字符 數值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000
例如, 羅馬數字 2 寫做 II ,即為兩個並列的 1。12 寫做 XII ,即為 X + II 。27 寫做 XXVII, 即為 XX + V + II 。
class Solution: def intToRoman(self, num: int) -> str: dic = {1000: "M", 500: "D", 100: "C", 50: "L", 10: "X", 5: "V", 1: "I"} bound = [1000, 500, 100, 50, 10, 5, 1] res = "" for i in range(0, 7, 2): if num >= bound[i]: n, num = divmod(num, bound[i]) if n >= 5: if n == 9: res += dic[bound[i]]+dic[bound[i-2]] else: res += dic[bound[i-1]]+(n-5)*dic[bound[i]] else: if n == 4: res += dic[bound[i]] + dic[bound[i - 1]] else: res += n * dic[bound[i]] else: continue return res
93. 復原IP位址
難度:中等
給定一個只包含數字的字符串,復原它並返回所有可能的 IP 地址格式。
有效的 IP 地址 正好由四個整數(每個整數位於 0 到 255 之間組成,且不能含有前導 0),整數之間用 '.'分隔。
例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "192.168@1.1" 是 無效的 IP 地址。
class Solution: def longestPalindrome(self, s: str) -> str: def isPalindrome(s: str) -> bool: left = 0 right = len(s)-1 while left < right: if s[left] != s[right]: return False left += 1 right -= 1 return True
max_len = 0 for i in range(len(s)): if max_len >= len(s) - i: break for j in reversed(range(len(s))): if isPalindrome(s[i:j + 1]): if max_len < j - i + 1: max_len = j - i + 1 res = s[i:j + 1] break return res 思路2:中心擴展算法
class Solution: def expandAroundCenter(self, s, left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return left + 1, right - 1
def longestPalindrome(self, s: str) -> str: start, end = 0, 0 for i in range(len(s)): left1, right1 = self.expandAroundCenter(s, i, i) left2, right2 = self.expandAroundCenter(s, i, i + 1) if right1 - left1 > end - start: start, end = left1, right1 if right2 - left2 > end - start: start, end = left2, right2 return s[start: end + 1] 思路3:動態規劃leetcode上面的官方題解
我們用 表示字符串 s 的第 i 到 j 個字母組成的串是否為回文串:
那麼我們可以寫出狀態轉移方程:
明顯發現,上面的狀態轉移方程只適用於j-i>=3這種情況,那麼我們還要考慮小於3的情況:
時間複雜度: ,空間複雜度:
結果:超時
代碼
class Solution: def longestPalindrome(self, s: str) -> str: len_s = len(s) dp = [[False] * len_s for _ in range(len_s)] ans = "" #子序列從長度為0開始遍歷 for len_subseq in range(len_s): #子序列首字母i for i in range(len_s): #子序列尾字母j j = i+len_subseq if j >= len_s: break if len_subseq == 0: dp[i][j] = True elif len_subseq == 1: dp[i][j] = (s[i] == s[j]) else: dp[i][j] = (dp[i+1][j-1]) & (s[i] == s[j]) if dp[i][j] & (len_subseq+1>len(ans)): ans = s[i:j+1] return ans 動態規劃+分類討論
class Solution: def expand(self, s, left, right): while left >= 0 and right < len(s) and s[left] == s[right]: left -= 1 right += 1 return (right - left - 2) // 2
def longestPalindrome(self, s: str) -> str: end, start = -1, 0 s = '#' + '#'.join(list(s)) + '#' arm_len = [] right = -1 j = -1 for i in range(len(s)): if right >= i: i_sym = 2 * j - i min_arm_len = min(arm_len[i_sym], right - i) cur_arm_len = self.expand(s, i - min_arm_len, i + min_arm_len) else: cur_arm_len = self.expand(s, i, i) arm_len.append(cur_arm_len) if i + cur_arm_len > right: j = i right = i + cur_arm_len if 2 * cur_arm_len + 1 > end - start: start = i - cur_arm_len end = i + cur_arm_len return s[start+1:end+1:2] 參考文獻leetcode中文官網:https://leetcode-cn.com/
所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !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.https://leetcode-cn.com
所以,為了提高大家的算法能力,這個公眾號後續每天帶大家做一道算法題,題目就從LeetCode上面選 !今天和大家聊的問題叫做 單詞拆分,我們先來看題面:https://leetcode-cn.com/problems/word-break/Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be
分享一下身邊大神的刷題順序:如果你時間比較緊迫,為了找工作而刷題,我建議你先刷熱門推薦,一共兩百多道題。先刷熱題 HOT 100,再刷精選 TOP 面試題,之後刷其他的題。如果你時間比較充裕,那我建議你:按從低到高的難度分組刷按 tag 分類刷定期複習,重做之前刷過的題掌握 LeetCode 刷題方法再開始刷題,屬於磨刀不誤砍柴工。