756. 金字塔转换矩阵



"""
学到的新知识:
from collections import defaultditc可以帮我们初始化字典,不至于取到某个不存在的值的时候报错。例如列表类型就会默认初始值为[],str对应的是空字符串,set对应set( ),int对应0 思路:
通过本层构建上一层(DFS,类似于全排列),看是否能构建成功(递归) """
from collections import defaultdict
class Solution:
def pyramidTransition(self, bottom: str, allowed) -> bool:
# 先将所有能建成的放在一起,这样就节省找的时间
mat = defaultdict(list)
for i in allowed:
mat[i[:2]].append(i[-1])
return self.DFS(bottom, mat) def DFS(self, bottom, mat):
# 如果最后只剩下一个,那肯定就是根了
if len(bottom) <= 1:
return True
candidate = []
# 通过这一层构建上一层的每一种解决方案,也是一个DFS的过程,类似于全排列
def generateUpper(bottom, tmp, ind, mat):
if ind == len(bottom) - 1:
candidate.append(tmp.copy())
return
if mat.get(bottom[ind] + bottom[ind + 1]):
for j in mat.get(bottom[ind] + bottom[ind + 1]):
tmp.append(j)
generateUpper(bottom, tmp, ind + 1, mat)
tmp.remove(j)
generateUpper(bottom, [], 0, mat)
# 判断解决方案中是否有成立的
for i in candidate:
if self.DFS(i, mat):
return True
return False

1034. 边框着色

"""
思路:
首先弄清楚边界的概念:
①是整个矩阵的第一列or最后一列or第一行or最后一行
②其四周有其他的颜色(表示和其他连通分量相邻)
即如果该块的四周有不同颜色块或者位于边界才染色,否则只是经过,并不染色。对第一个要特殊判断一下。
"""
import copy
class Solution:
def colorBorder(self, grid, r0: int, c0: int, color: int):
# 用一个新grid的来染色
newgrid = copy.deepcopy(grid)
self.DFS(r0, c0, [], grid, color, newgrid)
# 对第一个特殊判断一下
if not self.judge(r0, c0, grid):
newgrid[r0][c0] = grid[r0][c0]
else:
newgrid[r0][c0] = color
return newgrid def DFS(self, x, y, vis, grid, color, newgrid):
directx = [-1, 1, 0, 0]
directy = [0, 0, -1, 1]
# 遍历其附近的结点
for i in range(4):
newx, newy = x + directx[i], y + directy[i]
if -1 < newx < len(grid) and -1 < newy < len(grid[0]):
if (newx, newy) not in vis and grid[newx][newy] == grid[x][y]:
# 只有是边界才染色,否则只是走过去,而不染色
if self.judge(newx, newy, grid):
newgrid[newx][newy] = color
self.DFS(newx, newy, vis + [(newx, newy)], grid, color, newgrid) def judge(self, x, y, grid):
# 判断是否为边界
if x == 0 or x == len(grid)-1 or y == 0 or y == len(grid[0])-1:
return True
directx = [-1, 1, 0, 0]
directy = [0, 0, -1, 1]
# 判断是否与其他连通分量相邻
for i in range(4):
newx, newy = x + directx[i], y + directy[i]
if -1 < newx < len(grid) and -1 < newy < len(grid[0]):
if grid[newx][newy] != grid[x][y]:
return True
return False

1110. 删点成林

"""
思路:
本题重点就是后序遍历
本题卡住的点:
① 删除结点间有前后关系 -> 所以选择后序遍历从后往前删除
② 需要删除根节点的 -> 判断一下根节点是否在需要删除的列表中
"""
class Solution:
def delNodes(self, root: TreeNode, to_delete: List[int]) -> List[TreeNode]:
res = []
# 后序遍历(lastNode为现在结点的前一个结点,check表示这个是前一个结点的左子树or右子树),从子节点往父节点删除
def DFS(root, lastNode, check):
if root:
DFS(root.left, root, 'l')
DFS(root.right, root, 'r')
# 如果找到了需要删除的元素,就把它的左右儿子加到结果列表中
if root.val in to_delete:
if root.left:
res.append(root.left)
if root.right:
res.append(root.right)
# 根据check和lastNode,在原始树中将这个结点置为None
if check == 'l':
lastNode.left = None
elif check == 'r':
lastNode.right = None
DFS(root, None, '-')
# 如果根节点没有被删除,就把修改过的整棵树添加到结果集中
if root.val not in to_delete:
res.append(root)
return res

491. 递增子序列

"""
思路:
题目就是DFS,但是一直超时,还以为是方法有问题,最后竟然只是去重的问题(我屮艸芔茻
好在这道题目没有考虑内部顺序的问题or运气好?直接用set对二维数组去重竟然过了测试用例emmm
"""
class Solution:
def findSubsequences(self, nums):
res = []
vis = set()
def DFS(ind, tmp):
if len(tmp) > 1:
# 如果在这里用not in去重,一定会超时。
res.append(tmp)
# 对于每个数字都有两种选择:选or不选
for i in range(ind, len(nums)):
if len(tmp) == 0 or nums[i] >= tmp[-1]:
if i not in vis:
vis.add(i)
DFS(i + 1, tmp+[nums[i]])
vis.remove(i)
DFS(0, [])
# 对二维数组去重,先将每个列表都转换为元组再去重再转换为列表
res = list(set([tuple(t) for t in res]))
res = [list(v) for v in res]
return res

721. 账户合并

"""
思路:
需要注意一下的是这里返回的时候需要对结果排序
"""
from collections import defaultdict
class Solution:
def accountsMerge(self, accounts):
# 建图
mat = defaultdict(list)
for i in range(len(accounts)):
for j in range(1, len(accounts[i])-1):
mat[accounts[i][j]].append(accounts[i][j+1])
mat[accounts[i][j+1]].append(accounts[i][j]) def DFS(email):
if not mat.get(email):
return
for i in mat[email]:
if i not in vis:
vis.add(i)
res.append(i)
DFS(i) vis = set()
newAcc = []
"""
每次对某个人的第一个邮箱开始进行遍历,res存储邮箱走过的路径。
如果res为空说明这个人只有一个邮箱,如果res等于这个人的原始邮箱(这两种情况都说明没有和其他人关联),
否则res长度一定大于原始邮箱长度,这说明加入了其他邮箱,将这个新邮箱赋值过去
"""
for i in range(len(accounts)):
# 如果这个人的第一个邮箱已经被访问过了,说明这个人是重复的。
if accounts[i][1] in vis:
continue
res = []
DFS(accounts[i][1])
# 要么是自己本身的邮箱,要么就是拓展后的所有邮箱
if len(res) != 0:
newAcc.append([accounts[i][0]] + sorted(res))
# 只有一个邮箱
else:
newAcc.append(accounts[i])
return newAcc

988. 从叶结点开始的最小字符串

"""
本题 = 记录根到叶子结点的所有路径,然后再找到最小的即可。
"""
class Solution:
def smallestFromLeaf(self, root: TreeNode) -> str:
res = []
# 找到所有路径
def DFS(root, tmp):
if not root:
return
if not root.left and not root.right:
res.append((tmp+[root.val]).copy()[::-1])
return
DFS(root.left, tmp + [root.val])
DFS(root.right, tmp + [root.val])
# 返回最小的
DFS(root, [])
s = ""
for i in min(res):
s += ord(i + 97)
return s

Leetcode题解 - DFS部分题目代码+思路(756、1034、1110、491、721、988)的更多相关文章

  1. Leetcode题解 - BFS部分题目代码+思路(896、690、111、559、993、102、103、127、433)

    和树有关的题目求深度 -> 可以利用层序遍历 -> 用到层序遍历就想到使用BFS 896. 单调数列 - 水题 class Solution: def isMonotonic(self, ...

  2. Leetcode题解 - 树、DFS部分简单题目代码+思路(700、671、653、965、547、473、46)

    700. 二叉搜索树中的搜索 - 树 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 思路: 二 ...

  3. Leetcode题解 - DFS部分简单题目代码+思路(113、114、116、117、1020、494、576、688)

    这次接触到记忆化DFS,不过还需要多加练习 113. 路径总和 II - (根到叶子结点相关信息记录) """ 思路: 本题 = 根到叶子结点的路径记录 + 根到叶子结点 ...

  4. Leetcode题解 - 贪心算法部分简单题目代码+思路(860、944、1005、1029、1046、1217、1221)

    leetcode真的是一个学习阅读理解的好地方 860. 柠檬水找零 """ 因为用户支付的只会有5.10.20 对于10元的用户必须找一个5 对于20元的用户可以找(三 ...

  5. Leetcode题解 - 树部分简单题目代码+思路(105、106、109、112、897、257、872、226、235、129)

    树的题目中递归用的比较多(但是递归是真难弄 我

  6. Leetcode题解 - 链表简单部分题目代码+思路(21、83、203、206、24、19、876)

  7. 【LeetCode题解】二叉树的遍历

    我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...

  8. [LeetCode 题解] Combination Sum

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a se ...

  9. [LeetCode 题解]: Triangle

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given a tr ...

随机推荐

  1. sed 使用介绍

    第6周第4次课(4月26日) 课程内容: 9.4/9.5 sed 9.4/9.5 sed 操作实例如下 sed和grep比较起来,sed也可以实现grep的功能,但是没有颜色显示,sed强项是替换一些 ...

  2. WIN2003+IIS6环境SSL证书的安装

        下载LOFTER我的照片书  |     一.解压证书文件.证书文件解压后,找到后缀为.pfx的压缩包,进行解压到固定位置.(一般放在网站根目录)        

  3. Netty学习——Thrift的入门使用

    Netty学习——Thrift的入门使用 希望你能够,了解并使用它.因为它是一个效率很高的框架 官网地址:http://thrift.apache.org/ 1.Thrift数据类型 一门技术如果需要 ...

  4. lerna式升级

    有段时间没更新博客了,是时候更新一波了. 之前不是vue-next出了吗,然后就去学习了一下,发现整个目录不是那么熟悉了,变成这样了: 于是就这个线索去研究了一下,发下这是用的 lerna + yar ...

  5. Mysql的查询语句的使用

    1. 简单查询 查询所有字段: SELECT * FROM 表名 查询特定字段: SELECT 字段列表 FROM 表名 2. 查询显示行号 - 在字段列表中加入(@rownum := @rownum ...

  6. luogu P2863 [USACO06JAN]牛的舞会The Cow Prom |Tarjan

    题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their ...

  7. 在 Xcode9 中自定义文件头部注释和其他文本宏

    在 Xcode9 中自定义文件头部注释和其他文本宏 . 参考链接 注意生成的plist文件的名称为IDETemplateMacros.plist 在plist文件里设置自己想要的模板 注意plist存 ...

  8. 尺取法two pointers

    目的:对给定的一个序列,在序列中寻找包含全部需求的.长度最小的一段子序列.一般用来解决具有单调性的区间问题. 时间复杂度:O(n) https://blog.csdn.net/lxt_lucia/ar ...

  9. 导出excel时,跳转新空白页,不要跳转怎么改

    导出excel的时候,偶尔会出现跳转到一个新页面再导出excel js中用window.open()做跳转 不想让它跳转到新页面,需要加一个隐藏的iframe <iframe name=&quo ...

  10. 源码分析 RocketMQ DLedger(多副本) 之日志复制(传播)

    目录 1.DLedgerEntryPusher 1.1 核心类图 1.2 构造方法 1.3 startup 2.EntryDispatcher 详解 2.1 核心类图 2.2 Push 请求类型 2. ...