78-82逐渐习惯两星期一写..
78:子集问题
我用的是深度回溯 结果空间大爆炸
思路简单 类似于层序遍历的思想 这棵树每层都是一个下标。
第一层 空 第二层 是0到n的下标 添加到结果
结果中 第一个到第n+1 个是第一层的 把他们都挑出来画子树 用curpos记录第三层的位置
然后再遍历 第三层的 pos2 到第三层下标就是新的一层。 他们成为了新的pos1 pos2
为什么记录下表 为了不重复啊 只能记录下标了 委屈。。。
但是我看有一个很简单的写法,对于i从1到n 对前面的 列表中的每一个元素后添加一个i 并加入列表
class Solution: def subsets(self, nums: List[int]) -> List[List[int]]: rel = [[]] n = len(nums) for i in range(n): rel.append([i]) pos1 = 1 pos2 = n+1 #从rel的 1到n+1 个是第一层 即只有一位长度的 def dfs(pos1,pos2): curpos = pos2 for each in range(pos1,pos2): if rel[each][-1] == n: continue for j in range(rel[each][-1]+1,n): temp = rel[each][:] temp.append(j) rel.append(temp) #遍历新层是在上层的基础上 加一个数就行。 curpos += 1 return pos2,curpos #pos2 到curpos成为了新的一层 for i in range(n-1): pos1,pos2 = dfs(pos1,pos2) for each in rel[1:]: for i,index in enumerate(each): each[i] = nums[index] #没想到其他方法防止重复 只能用下标了 return rel 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/subsets/solution/shi-jian-huan-xing-kong-jian-da-bao-zha-a60gb/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
79
单词搜索
看的答案 没想到要这样
参考之前扫雷的思路,每到一个点就往前后左右找 但记得用VISIT记录、
class Solution: def exist(self, board: List[List[str]], word: str) -> bool: nrow = len(board) nw = len(word) ncol = len(board[0]) visited = [[0]*ncol for i in range(nrow)] def check(i,j,index): visited[i][j] = 1 if index == nw-1 and board[i][j] == word[index]: return True if board[i][j] == word[index]: if i-1 >= 0 and visited[i-1][j] == 0: if check(i-1,j,index+1): return True if i+1 < nrow and visited[i+1][j] == 0: if check(i+1,j,index+1): return True if j+1 < ncol and visited[i][j+1] == 0: if check(i,j+1,index+1): return True if j-1 >= 0 and visited[i][j-1] == 0: if check(i,j-1,index+1): return True visited[i][j] = 0 return False for i in range(nrow): for j in range(ncol): if board[i][j] == word[0]: if check(i,j,0): return True return False 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/word-search/solution/wo-du-bu-zhi-dao-zen-yao-da-bai-zhe-yao-hw7kv/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
80 : 删除有序数组重复项
用rep记录重复次数
用ready记录合法的长度
last记录最后一个合法的数
i来遍历 n来记录长度
空间被大部分人击败
class Solution: def removeDuplicates(self, nums: List[int]) -> int: n = len(nums) ready = 0 rep = 0 last = nums[0]-1 i = 0 while i < n: if nums[i] == last: rep += 1 if rep == 2: rep = 0 while nums[i] == last: i += 1 if i == n: return ready else: rep = 0 nums[ready] = nums[i] last = nums[i] ready += 1 i += 1 return ready 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array-ii/solution/po-shi-wu-hua-by-yizhu-jia-ypfv/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
81 搜索旋转数组 2
与之前的相比 这道题要添加一个等于左右的判断
class Solution: def search(self, nums: List[int], target: int) -> bool: if target in nums: return True return False n = len(nums) r = n-1 l = 0 while l <= r: mid = (l+r)//2 temp = nums[mid] if temp == target: return True if temp == nums[r] and temp == nums[l]: for each in nums[l:r+1]: if each == target: return True #防止 111112111 t = 2当这样的出现时 你不知道该向左还是向右 只能遍历 elif temp == nums[r]: r = mid -1 #要不然 右边全等 肯定在左边 elif temp == nums[l]: #左边全等 在右边 l = mid +1 elif temp < nums[r]: #类似之前判断 if target <= nums[r]: l = mid + 1 else: r = mid - 1 else: if target >= nums[l]: r = mid - 1 else: l = mid + 1 return False 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/e-wu-yu-liao-wo-du-tian-jia-yi-ge-deng-y-czhu/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
82 删除排序链表重复元素
这种题我都会加头节点的
# Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = next class Solution: def deleteDuplicates(self, head: ListNode) -> ListNode: if head == None : return None headnode = ListNode(head.val-1,head) p = head q = headnode r = headnode while p.next != None : if p.val != p.next.val and p.val != q.val: r.next = p r = p p = p.next q = q.next if p .val != q.val : r.next = p else: r.next = None return headnode.next 作者:yizhu-jia 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii/solution/ji-bai-bai-fen-zhi-95-yi-ge-zhi-qian-mia-a2w2/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

浙公网安备 33010602011771号