123-128 这周继续理财 你不理财 财不理你
123 买股票最佳时机3 看了好久的题解才看懂
class Solution: def maxProfit(self, prices: List[int]) -> int: if prices == []: return 0 n = len(prices) buy1 = prices[0] buy2 = prices[0] sell1 = 0 sell2 = 0 for i in range(1,n): buy1 = min(buy1,prices[i]) sell1 = max(sell1,prices[i]-buy1) buy2 = min(buy2,prices[i]-sell1) sell2 = max(sell2,prices[i]-buy2) return sell2 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/ti-jiao-wu-ci-dou-bu-guo-zhi-neng-kan-ti-w4jl/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
125:验证回文串 简单题 无限错 唉 就是玩 ‘
用ASCII操作
class Solution: def isPalindrome(self, s: str) -> bool: def isnum(letter): a = ord(letter) if (a >= 65 and a <= 90) or ( a >= 48 and a <= 57) or ( a >= 97 and a <= 122): return True return False n = len(s) left = 0 right = n - 1 while left < right: while not isnum(s[left]) : left += 1 if left >= n : return True while not isnum(s[right]): right -= 1 if right < 0: return True if left > right : return True if s[left].lower() != s[right].lower(): return False left += 1 right -= 1 return True 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/valid-palindrome/solution/jian-dan-ti-wu-xian-cuo-ai-jiu-shi-wan-b-gllm/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
126:
单词接龙哈 难得我直接化身CV工程师
开始就觉得像个什么 后面才知道 这是图啊
from collections import defaultdict from typing import List from collections import deque import string class Solution: def findLadders(self, beginWord: str, endWord: str, wordList: List[str]) -> List[List[str]]: # 先将 wordList 放到哈希表里,便于判断某个单词是否在 wordList 里 word_set = set(wordList) res = [] if len(word_set) == 0 or endWord not in word_set: return res successors = defaultdict(set) # 第 1 步:使用广度优先遍历得到后继结点列表 successors # key:字符串,value:广度优先遍历过程中 key 的后继结点列表 found = self.__bfs(beginWord, endWord, word_set, successors) if not found: return res # 第 2 步:基于后继结点列表 successors ,使用回溯算法得到所有最短路径列表 path = [beginWord] self.__dfs(beginWord, endWord, successors, path, res) return res def __bfs(self, beginWord, endWord, word_set, successors): queue = deque() queue.append(beginWord) visited = set() visited.add(beginWord) found = False word_len = len(beginWord) next_level_visited = set() while queue: current_size = len(queue) for i in range(current_size): current_word = queue.popleft() word_list = list(current_word) for j in range(word_len): origin_char = word_list[j] for k in string.ascii_lowercase: word_list[j] = k next_word = ''.join(word_list) if next_word in word_set: if next_word not in visited: if next_word == endWord: found = True # 避免下层元素重复加入队列 if next_word not in next_level_visited: next_level_visited.add(next_word) queue.append(next_word) successors[current_word].add(next_word) word_list[j] = origin_char if found: break # 取两集合全部的元素(并集,等价于将 next_level_visited 里的所有元素添加到 visited 里) visited |= next_level_visited next_level_visited.clear() return found def __dfs(self, beginWord, endWord, successors, path, res): if beginWord == endWord: res.append(path[:]) return if beginWord not in successors: return successor_words = successors[beginWord] for next_word in successor_words: path.append(next_word) self.__dfs(next_word, endWord, successors, path, res) path.pop() 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/word-ladder-ii/solution/qi-de-wo-zhi-jie-bian-shen-cvgong-cheng-sll3k/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
127 : 接龙1 :
广度遍历 你值得拥有
from collections import defaultdict from typing import List from collections import deque import string class Solution: def ladderLength(self, beginWord, endWord, wordList): wordset = set(wordList) if endWord not in wordset: return 0 wordlen = len(beginWord) queue = deque() visited = set() visited.add(beginWord) queue.append(beginWord) pathlen = 0 while queue: pathlen += 1 quesize = len(queue) for i in range(quesize): curword = queue.popleft() for j in range(wordlen): curwordlist = list(curword) for k in string.ascii_lowercase: curwordlist[j] = k changeword = ''.join(curwordlist) if changeword == endWord: return pathlen +1 if changeword not in visited: if changeword in wordset: queue.append(changeword) visited.add(changeword) return 0 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/word-ladder/solution/yan-du-you-xian-bian-li-zuo-1chao-2de-zh-7v1k/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
128:最长连续序列
还是没想出来O(n)的解法
只得问题姐
class Solution: def longestConsecutive(self, nums: List[int]) -> int: numset = set(nums) maxlen = 0 for each in nums: curlen = 1 if each-1 in numset: continue else: while each+1 in numset: curlen += 1 each += 1 if curlen > maxlen : maxlen = curlen return maxlen 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/longest-consecutive-sequence/solution/dui-bu-qi-mei-xiang-chu-lai-by-yizhu-jia-u42u/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。