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

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

🔗
hey123_1 2021-5-9 14:14:12 | 只看该作者
全局:
Day 2

最近身体很差,但是也要保证大脑锻炼。

9:05PM - 11:13PM 中途无聊的打了三场王者。。。没关系,打游戏也算是锻炼大脑。

https://leetcode.com/problems/word-break-ii/
  1. class Solution(object):
  2.     def wordBreak(self, s, wordDict):
  3.         """
  4.         :type s: str
  5.         :type wordDict: List[str]
  6.         :rtype: List[str]
  7.         """
  8.         mems = {}
  9.         wordDict = set(wordDict)
  10.         def dfs(s):
  11.             if s in mems:
  12.                 return mems[s]
  13.             res = []
  14.             if s in wordDict:
  15.                 res.append(s)
  16.             for i in range(len(s)):
  17.                 right = s[i:]
  18.                 if right in wordDict:
  19.                     res += [w+ ' ' + right for w in dfs(s[:i])]
  20.             mems[s] = res
  21.             return mems[s]
  22.         return dfs(s)
  23.                
复制代码



https://leetcode.com/problems/sudoku-solver/

  1. class Solution(object):
  2.     def solveSudoku(self, board):
  3.         """
  4.         :type board: List[List[str]]
  5.         :rtype: None Do not return anything, modify board in-place instead.
  6.         """
  7.         self.board = board
  8.         return self.dfs(0, 0)
  9.    
  10.     def dfs(self, row, col):
  11.         if row == 9:
  12.             return True
  13.         if col >= 9:
  14.             return self.dfs(row+1, 0)
  15.         if self.board[row][col] != '.':
  16.             return self.dfs(row, col+1)
  17.         for i in range(1, 10):
  18.             c = str(i)
  19.             if not self.isValid(row, col, c):
  20.                 continue
  21.             self.board[row][col] = c
  22.             if self.dfs(row, col+1):
  23.                 return True
  24.             self.board[row][col] = '.'
  25.         return False
  26.             
  27.    
  28.     def isValid(self, row, col, val):
  29.         # row and column
  30.         for i in range(9):
  31.             if self.board[i][col] == val:
  32.                 return False
  33.             if self.board[row][i] == val:
  34.                 return False
  35.         grid_row = row - row%3
  36.         grid_col = col - col%3
  37.         for i in range(3):
  38.             for j in range(3):
  39.                 if self.board[grid_row+i][grid_col+j] == val:
  40.                     return False
  41.         return True
复制代码


回复

使用道具 举报

🔗
hey123_1 2021-5-9 14:39:03 | 只看该作者
全局:
Day 2
11:21PM 不是很困,打算刷到困就去睡觉。leetcode 现在一共1162题。 每天刷十道题就可以两个月不到全部刷完。
我来试试我能不能真的两个月全部刷完。 做不到也没关系。。。就是为了锻炼大脑。

https://leetcode.com/problems/word-search-ii/

  1. import collections

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

  6. class Trie:
  7.     def __init__(self):
  8.         self.root = TrieNode()
  9.     def insert(self, word):
  10.         node = self.root
  11.         for l in word:
  12.             node = node.children[l]
  13.         node.isWord = word
  14. class Solution(object):
  15.     def findWords(self, board, words):
  16.         """
  17.         :type board: List[List[str]]
  18.         :type words: List[str]
  19.         :rtype: List[str]
  20.         """
  21.         rowN, colN = len(board), len(board[0])
  22.         trie = Trie()
  23.         for w in words:
  24.             trie.insert(w)
  25.         
  26.         res = []
  27.         def dfs(node, row, col, res):
  28.             
  29.             if node.isWord:
  30.                 res.append(node.isWord)
  31.                 node.isWord = False
  32.             if row < 0 or col < 0 or row >= rowN or col >= colN:
  33.                 return
  34.             char = board[row][col]
  35.             node_child = node.children.get(char)
  36.             if not node_child:
  37.                 return
  38.             board[row][col] = '#'
  39.             for (i, j) in [(0,1), (0,-1), (1,0), (-1,0)]:
  40.                 dfs(node_child, row+i, col+j, res)
  41.             board[row][col] = char
  42.             
  43.         for i in range(rowN):
  44.             for j in range(colN):
  45.                 dfs(trie.root, i, j, res)
  46.         return res
  47.                
  48.         
复制代码

回复

使用道具 举报

全局:
可以来加入我们
回复

使用道具 举报

全局:
请问怎么加楼主微信呢?
回复

使用道具 举报

全局:
恋斌 发表于 2021-05-08 03:55:55
我想请问,刷题有标准答案吗?我都只看到题目没有看到代码
没有,输出是对的,时间 空间不超过就可
回复

使用道具 举报

全局:
ZYwang_one 发表于 2021-05-09 08:21:54
没有,输出是对的,时间 空间不超过就可
那问一下,想看答案,是只能看别人的解题思路了吗。。。
回复

使用道具 举报

🔗
hey123_1 2021-5-10 01:05:37 | 只看该作者
全局:

怎么加入呀 。
回复

使用道具 举报

🔗
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 -
回复

使用道具 举报

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

本版积分规则

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