楼主: 匿名
跳转到指定楼层
上一主题 下一主题
收起左侧

[找小伙伴一起刷题] 一起锻炼大脑。

🔗
hey123_1 2021-5-10 02:23:47 | 只看该作者
全局:
Day 3
今天起床比较晚。吃了两片面包。开始刷,等下出去玩。

这里把一系列的combination都做掉好了。

10:09AM - 10:26AM
https://leetcode.com/problems/combination-sum/
  1. class Solution(object):
  2.     def combinationSum(self, candidates, target):
  3.         """
  4.         :type candidates: List[int]
  5.         :type target: int
  6.         :rtype: List[List[int]]
  7.         """
  8.         res = []
  9.         def dfs(index, current, target):
  10.             if target == 0:
  11.                 res.append(list(current))
  12.             if target < 0:
  13.                 return
  14.             for i in range(index, len(candidates)):
  15.                 current.append(candidates[i])
  16.                 dfs(i, current, target-candidates[i])
  17.                 current.pop()
  18.         dfs(0, [], target)
  19.         return res
复制代码


10:26AM - 10:29AM
https://leetcode.com/problems/combinations/
  1. class Solution(object):
  2.     def combine(self, n, k):
  3.         """
  4.         :type n: int
  5.         :type k: int
  6.         :rtype: List[List[int]]
  7.         """
  8.         res = []
  9.         
  10.         def dfs(index, current):
  11.             if len(current) == k:
  12.                 res.append(list(current))
  13.             for i in range(index+1, n+1):
  14.                 current.append(i)
  15.                 dfs(i, current)
  16.                 current.pop()
  17.         dfs(0, [])
  18.         return res
  19.         
复制代码


https://leetcode.com/problems/combination-sum-ii/
10:29AM - 11:05AM
这道题和之前的combination sum最大的区别是不能有重复的数字。
  1. class Solution(object):
  2.     def combinationSum2(self, candidates, target):
  3.         """
  4.         :type candidates: List[int]
  5.         :type target: int
  6.         :rtype: List[List[int]]
  7.         """
  8.         candidates = sorted(candidates)
  9.         res = []
  10.         print(candidates)
  11.         def dfs(index, current, target):
  12.             if target == 0:
  13.                 res.append(list(current))
  14.             if target < 0:
  15.                 return
  16.             for i in range(index, len(candidates)):
  17.                 if candidates[i] > target:
  18.                     break
  19.                 if i > index and candidates[i] == candidates[i-1]:
  20.                     continue
  21.                 current.append(candidates[i])
  22.                 dfs(i+1, current, target-candidates[i])
  23.                 current.pop()
  24.         dfs(0, [], target)
  25.         return res
  26.         
复制代码



https://leetcode.com/problems/combination-sum-iii/

11:06AM - 11:13AM
  1. class Solution(object):
  2.     def combinationSum3(self, k, n):
  3.         """
  4.         :type k: int
  5.         :type n: int
  6.         :rtype: List[List[int]]
  7.         """
  8.         res = []
  9.         
  10.         def dfs(current_number, current, target):
  11.             if target == 0 and len(current) == k:
  12.                 res.append(list(current))
  13.             if target < 0 or len(current) >= k:
  14.                 return
  15.             for i in range(current_number, 10):
  16.                 if i > target:
  17.                     break
  18.                 dfs(i+1, current+[i], target-i)
  19.         dfs(1, [], n)
  20.         return res
  21.             
  22.                     
  23.         
复制代码


回复

使用道具 举报

🔗
hey123_1 2021-5-10 02:32:42 | 只看该作者
全局:
Day 3
https://leetcode.com/problems/combination-sum-iv/

11:13AM - 11:30AM
  1. class Solution(object):
  2.     def combinationSum4(self, nums, target):
  3.         """
  4.         :type nums: List[int]
  5.         :type target: int
  6.         :rtype: int
  7.         """
  8.         nums = sorted(nums)
  9.         mem = {0:1}
  10.         def calculate(remain, mem):
  11.             if remain in mem:
  12.                 return mem[remain]
  13.             res = 0
  14.             for n in nums:
  15.                 if remain - n < 0:
  16.                     break
  17.                 res += calculate(remain-n, mem)
  18.             mem[remain] = res
  19.             return res
  20.         calculate(target, mem)
  21.         return mem[target]
  22.                
  23.                     
  24.         
复制代码

回复

使用道具 举报

无效楼层,该帖已经被删除
无效楼层,该帖已经被删除
地里匿名用户
🔗
匿名用户-RMMGT  2021-5-10 04:26:15
Day 3

刚才刷了全部combination sum一共四道题。现在回来刷backtracking, 还差四道题就刷完backtracking的绿色部分了。我刷到两点出门玩~ 开森。

https://leetcode.com/problems/wildcard-matching/
11:38AM - 12:23PM

这是一道好烦人的题,重要的是要记住s和p的星星开始的位置。
  1. class Solution(object):
  2.     def isMatch(self, s, p):
  3.         """
  4.         :type s: str
  5.         :type p: str
  6.         :rtype: bool
  7.         """
  8.         si = pi = 0
  9.         stari = starsi = -1
  10.         while si<len(s):
  11.             if pi < len(p) and p[pi] in {'?', s[si]}:
  12.                 si+=1
  13.                 pi+=1
  14.             elif pi < len(p) and p[pi] == '*':
  15.                 stari = pi
  16.                 starsi = si
  17.                 pi += 1
  18.             elif stari > -1:
  19.                 pi = stari+1
  20.                 si = starsi+1
  21.                 starsi = si
  22.             else:
  23.                 return False
  24.         return all(x=='*' for x in p[pi:])
  25.         
复制代码


https://leetcode.com/problems/permutations/
12:24PM - 12:27PM
  1. class Solution(object):
  2.     def permute(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: List[List[int]]
  6.         """
  7.         res = []
  8.         visited = set()
  9.         def dfs(cur):
  10.             if len(cur) == len(nums):
  11.                 res.append(list(cur))
  12.             for n in nums:
  13.                 if n in visited:
  14.                     continue
  15.                 visited.add(n)
  16.                 dfs(cur+[n])
  17.                 visited.remove(n)
  18.         dfs([])
  19.         return res
复制代码


https://leetcode.com/problems/de ... rds-data-structure/
一看到这种题就是要用trie
12:28PM - 1:23PM 刚才顺便吃了个饭

  1. class TrieNode:
  2.     def __init__(self):
  3.         self.children = collections.defaultdict(TrieNode)
  4.         self.isWord = False
  5. class WordDictionary:

  6.     def __init__(self):
  7.         """
  8.         Initialize your data structure here.
  9.         """
  10.         self.root = TrieNode()
  11.         

  12.     def addWord(self, word: str) -> None:
  13.         node = self.root
  14.         for char in word:
  15.             node = node.children[char]
  16.         node.isWord = True
  17.    
  18.     def searchWord(self, word, node, index):
  19.         if index == len(word):
  20.             return node.isWord
  21.         if word[index] == '.':
  22.             for c in node.children.values():
  23.                 if c and self.searchWord(word, c, index+1):
  24.                     return True
  25.             return False
  26.         else:
  27.             return node.children[word[index]] and self.searchWord(word, node.children[word[index]], index+1)

  28.     def search(self, word: str) -> bool:
  29.         return self.searchWord(word, self.root, 0)
复制代码


https://leetcode.com/problems/n-queens/
这个N皇后问题其实蛮简单的重要的是可以写清楚什么时候放皇后是valid的情况。然后用dfs。
  1. class Solution:
  2.    
  3.     def isValid(self, row, col):
  4.         for k in range(row):
  5.             if self.queens[k][col] == 'Q':
  6.                 return False
  7.         for k in range(col):
  8.             if self.queens[row][k] == 'Q':
  9.                 return False
  10.         
  11.         for i, j in zip(range(row-1, -1, -1), range(col-1, -1, -1)):
  12.             if self.queens[i][j] == 'Q':
  13.                 return False
  14.         for i, j in zip(range(row-1, -1, -1), range(col+1, self.n)):
  15.             if self.queens[i][j] == 'Q':
  16.                 return False
  17.         return True
  18.    
  19.     def dfs(self, row):
  20.         if row == self.n:
  21.             self.res.append(self.format_queens())
  22.         for col in range(self.n):
  23.             if self.isValid(row, col):
  24.                 self.queens[row][col] = 'Q'
  25.                 self.dfs(row+1)
  26.                 self.queens[row][col] = '.'
  27.    
  28.     def format_queens(self):
  29.         res = []
  30.         for row in self.queens:
  31.             res.append(''.join(row))
  32.         return res
  33.    
  34.     def solveNQueens(self, n: int) -> List[List[str]]:
  35.         self.n = n
  36.         self.queens = [['.' for _ in range(n)] for _ in range(n)]
  37.         self.res = []
  38.         self.dfs(0)
  39.         return self.res
  40.         
  41.         
复制代码

1:24PM -
回复

使用道具 举报

🔗
hey123_1 2021-5-10 04:31:11 | 只看该作者
全局:
Day 3

刚才写了三道题不知道为啥没有了?

https://leetcode.com/problems/wildcard-matching/
这道题的重点是找到星号开始的地方
  1. class Solution(object):
  2.     def isMatch(self, s, p):
  3.         """
  4.         :type s: str
  5.         :type p: str
  6.         :rtype: bool
  7.         """
  8.         si = pi = 0
  9.         stari = starsi = -1
  10.         while si<len(s):
  11.             if pi < len(p) and p[pi] in {'?', s[si]}:
  12.                 si+=1
  13.                 pi+=1
  14.             elif pi < len(p) and p[pi] == '*':
  15.                 stari = pi
  16.                 starsi = si
  17.                 pi += 1
  18.             elif stari > -1:
  19.                 pi = stari+1
  20.                 si = starsi+1
  21.                 starsi = si
  22.             else:
  23.                 return False
  24.         return all(x=='*' for x in p[pi:])
  25.         
复制代码



https://leetcode.com/problems/permutations/
一秒做完
  1. class Solution(object):
  2.     def permute(self, nums):
  3.         """
  4.         :type nums: List[int]
  5.         :rtype: List[List[int]]
  6.         """
  7.         res = []
  8.         visited = set()
  9.         def dfs(cur):
  10.             if len(cur) == len(nums):
  11.                 res.append(list(cur))
  12.             for n in nums:
  13.                 if n in visited:
  14.                     continue
  15.                 visited.add(n)
  16.                 dfs(cur+[n])
  17.                 visited.remove(n)
  18.         dfs([])
  19.         return res
复制代码


https://leetcode.com/problems/de ... rds-data-structure/
这道题用Trie
  1. class TrieNode:
  2.     def __init__(self):
  3.         self.children = collections.defaultdict(TrieNode)
  4.         self.isWord = False
  5. class WordDictionary:

  6.     def __init__(self):
  7.         """
  8.         Initialize your data structure here.
  9.         """
  10.         self.root = TrieNode()
  11.         

  12.     def addWord(self, word: str) -> None:
  13.         node = self.root
  14.         for char in word:
  15.             node = node.children[char]
  16.         node.isWord = True
  17.    
  18.     def searchWord(self, word, node, index):
  19.         if index == len(word):
  20.             return node.isWord
  21.         if word[index] == '.':
  22.             for c in node.children.values():
  23.                 if c and self.searchWord(word, c, index+1):
  24.                     return True
  25.             return False
  26.         else:
  27.             return node.children[word[index]] and self.searchWord(word, node.children[word[index]], index+1)

  28.     def search(self, word: str) -> bool:
  29.         return self.searchWord(word, self.root, 0)
复制代码


https://leetcode.com/problems/n-queens/

这题其实不难,主要是把valid情况写清楚,然后用dfs
  1. class Solution:
  2.    
  3.     def isValid(self, row, col):
  4.         for k in range(row):
  5.             if self.queens[k][col] == 'Q':
  6.                 return False
  7.         for k in range(col):
  8.             if self.queens[row][k] == 'Q':
  9.                 return False
  10.         
  11.         for i, j in zip(range(row-1, -1, -1), range(col-1, -1, -1)):
  12.             if self.queens[i][j] == 'Q':
  13.                 return False
  14.         for i, j in zip(range(row-1, -1, -1), range(col+1, self.n)):
  15.             if self.queens[i][j] == 'Q':
  16.                 return False
  17.         return True
  18.    
  19.     def dfs(self, row):
  20.         if row == self.n:
  21.             self.res.append(self.format_queens())
  22.         for col in range(self.n):
  23.             if self.isValid(row, col):
  24.                 self.queens[row][col] = 'Q'
  25.                 self.dfs(row+1)
  26.                 self.queens[row][col] = '.'
  27.    
  28.     def format_queens(self):
  29.         res = []
  30.         for row in self.queens:
  31.             res.append(''.join(row))
  32.         return res
  33.    
  34.     def solveNQueens(self, n: int) -> List[List[str]]:
  35.         self.n = n
  36.         self.queens = [['.' for _ in range(n)] for _ in range(n)]
  37.         self.res = []
  38.         self.dfs(0)
  39.         return self.res
  40.         
  41.         
复制代码


好了 backtracking绿色frequency部分写完了。可以出去玩啦。开森。
回复

使用道具 举报

地里匿名用户
🔗
匿名用户-RMMGT  2021-5-10 04:36:05
不知道为什么没办法回复了。。。
回复

使用道具 举报

全局:

看我头像
回复

使用道具 举报

🔗
hey123_1 2021-5-10 13:24:08 | 只看该作者
全局:
改一下刷题记录的的格式。我不加答案了。。。

Day 3 2021-05-10
今天晚上的目标是干掉全部的BST。 7:00P到家吃饭吃掉五十分钟。。。

7:51PM-7:56PM https://leetcode.com/problems/validate-binary-search-tree/

7:56PM-8:23PM 中途去发了一会儿呆
https://leetcode.com/problems/inorder-successor-in-bst/

8:23PM - 8:36PM
我做法是创建iterator的时候全部inorder traverse一遍所有的node。
https://leetcode.com/problems/binary-search-tree-iterator/

8:36PM - 8:39PM
超级基础的一道题。。。。
https://leetcode.com/problems/search-in-a-binary-search-tree/

8:39PM - 8:43PM
https://leetcode.com/problems/insert-into-a-binary-search-tree/

8:43PM - 8:58PM
要找清楚BST每个node的前一个和后一个node是啥。
https://leetcode.com/problems/delete-node-in-a-bst/

9:07PM - 9:36PM
头好痛想睡觉了。。。 刷到困吧。
这道题,我之前用的heap做的。用bst的诀窍是,每个node存个count。
Kth Largest Element in a Stream

9:36PM - 9:43PM
https://leetcode.com/problems/lo ... r-of-a-binary-tree/

9:54PM 刷题有点无聊,😡 先去洗个澡。
10:08PM 洗完澡了。我洗澡好快啊。

10:08PM  
这道题就是,python里面没有treeset,是要自己写个bst的感觉。
https://leetcode.com/problems/contains-duplicate-iii/

10:16PM
https://leetcode.com/problems/balanced-binary-tree/

10:19PM
https://leetcode.com/problems/co ... binary-search-tree/
回复

使用道具 举报

全局:
恋斌 发表于 2021-05-09 08:31:21
那问一下,想看答案,是只能看别人的解题思路了吗。。。
leetcode上题解有很多解题思路的,比较详细,不只是代码。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表