随笔分类 -  算法

摘要:二分搜索模板 给一个有序数组和目标值,找第一次/最后一次/任何一次出现的索引,如果没有出现返回-1 模板四点要素 1、初始化:start=0、end=len-1 2、循环退出条件:start + 1 < end 3、比较中点和目标值:A[mid] ==、 <、> target 4、判断最后两个元素是 阅读全文 »
posted @ 2020-12-10 16:56 哈哈哈喽喽喽 阅读(78) 评论(0) 推荐(0) 编辑
摘要:https://github.com/labuladong/fucking-algorithm/blob/master/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%92%E7%B3%BB%E5%88%97/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%9 阅读全文 »
posted @ 2020-12-10 16:25 哈哈哈喽喽喽 阅读(86) 评论(0) 推荐(0) 编辑
摘要:1、95. 不同的二叉搜索树 II 考点: 递归思想 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val 阅读全文 »
posted @ 2020-12-10 11:10 哈哈哈喽喽喽 阅读(69) 评论(0) 推荐(0) 编辑
摘要:1、面试题 08.08. 有重复字符串的排列组合 class Solution: def permutation(self, S: str) -> List[str]: def bfs(cur_list): next_list = [] for cur in cur_list: cur_str, l 阅读全文 »
posted @ 2020-12-09 20:34 哈哈哈喽喽喽 阅读(144) 评论(0) 推荐(0) 编辑
摘要:1、面试题 08.14. 布尔运算 考点: 1、DFS class Solution: def countEval(self, s: str, result: int) -> int: self.ops = { '&': { True: [(True, True)], False: [(True, 阅读全文 »
posted @ 2020-12-09 20:15 哈哈哈喽喽喽 阅读(59) 评论(0) 推荐(0) 编辑
摘要:1、面试题 17.08. 马戏团人塔 class Solution: def bestSeqAtIndex(self, height: List[int], weight: List[int]) -> int: hei_wei = [] for idx, hei in enumerate(heigh 阅读全文 »
posted @ 2020-12-08 15:57 哈哈哈喽喽喽 阅读(83) 评论(0) 推荐(0) 编辑
摘要:1、面试题 02.02. 返回倒数第 k 个节点 考点: 1、collections.deque(maxlen=k)的使用 2、deque队尾插值append,队头取值的popleft()用法(或者result[0]) class Solution: def kthToLast(self, head 阅读全文 »
posted @ 2020-12-06 22:22 哈哈哈喽喽喽 阅读(95) 评论(0) 推荐(0) 编辑
摘要:1、剑指 Offer 50. 第一个只出现一次的字符 考点: class Solution: def firstUniqChar(self, s: str) -> str: ch_dict = {} for i in range(len(s)): cur = s[i] if cur in s[:i] 阅读全文 »
posted @ 2020-12-02 12:03 哈哈哈喽喽喽 阅读(83) 评论(0) 推荐(0) 编辑
摘要:1、1096. 花括号展开 II 考点: 用stack做,维持两个list:第一个list代表已经计算好的部分,第二个list代表增长中的未知部分看到字母就“乘入”第二个list,例如[a]*b变成“ab”,[a,c]*b就变成['ab','cb']看到“,”就代表第二个list已经不可能再继续增长 阅读全文 »
posted @ 2020-12-02 10:01 哈哈哈喽喽喽 阅读(107) 评论(0) 推荐(0) 编辑
摘要:1、399. 除法求值 考点: 1、使用并查集,计算出每个节点到跟节点的除法;如果2个数根节点不一致,则不能相除;如果是相同根节点, 相对除法为 2个数到根节点的相除 2、注意黄色字体的代码,因为返回的xx, yy均为tmp中的数据位置,所有这2行代码顺序不能错; 如果调换,先执行下面语句,则xx, 阅读全文 »
posted @ 2020-11-29 23:09 哈哈哈喽喽喽 阅读(83) 评论(0) 推荐(0) 编辑
摘要:1、面试题 17.10. 主要元素 https://leetcode-cn.com/problems/find-majority-element-lcci/ 考点: class Solution: def majorityElement(self, nums: List[int]) -> int: 阅读全文 »
posted @ 2020-11-21 22:55 哈哈哈喽喽喽 阅读(111) 评论(0) 推荐(0) 编辑
摘要:简单 1、111. 二叉树的最小深度 https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/ 考点: 1、一层一层遍历,直到满足要求 # Definition for a binary tree node. # class Tre 阅读全文 »
posted @ 2020-11-18 11:14 哈哈哈喽喽喽 阅读(95) 评论(0) 推荐(0) 编辑
摘要:1 124. 二叉树中的最大路径和 https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/ 考点: 1、后续遍历,根据左右节点和当前节点的值,判断当前节点最大路径的值 2、一个需要注意点是,左右节点+当前节点作为步骤1之外一个单独 阅读全文 »
posted @ 2020-11-04 23:30 哈哈哈喽喽喽 阅读(73) 评论(0) 推荐(0) 编辑
摘要:中等题 1、面试题 04.05. 合法二叉搜索树 https://leetcode-cn.com/problems/legal-binary-search-tree-lcci/ 考点: 1、按题意思是要比较当前节点和左右子树的值,也就是先要计算出左右子树的列表,才可知当前节点是否满足要求。 由此可知 阅读全文 »
posted @ 2020-10-23 00:25 哈哈哈喽喽喽 阅读(69) 评论(0) 推荐(0) 编辑
摘要:二叉树遍历分为前序、中序和后序遍历,其中在左子树肯定比右子树先遍历的条件下按根节点的位置确定是哪种遍历方式,即根节点在最开始遍历为先序遍历,根节点在中间遍历为中序遍历,根节点在后面遍历为后序遍历。 代码模板: # 递归方式 def perorder(root): if not root: retur 阅读全文 »
posted @ 2020-09-23 15:42 哈哈哈喽喽喽 阅读(156) 评论(0) 推荐(0) 编辑
摘要:1、栈 使用list实现,入栈有append,出栈使用pop >>> stack = [3, 4, 5] >>> stack.append(6) >>> stack.append(7) >>> stack [3, 4, 5, 6, 7] >>> stack.pop() 7 >>> stack [3, 阅读全文 »
posted @ 2020-09-23 15:22 哈哈哈喽喽喽 阅读(137) 评论(0) 推荐(0) 编辑
摘要:1、并查集是一种树形结构,用户处理不相交数据的合并和查询问题。 2.、并查集操作: a,find 查询是否是同一颗数 b,树合并 3、算法模板 # 递归查parent,并赋值 def find(self, i, relation): if not relation[i] == i: relation 阅读全文 »
posted @ 2020-05-05 01:05 哈哈哈喽喽喽 阅读(61) 评论(0) 推荐(0) 编辑
摘要:1、栈题的时候,pop之前需要判断栈是否为空 https://leetcode-cn.com/problems/simplify-path/submissions/ 阅读全文 »
posted @ 2020-03-04 21:23 哈哈哈喽喽喽 阅读(162) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示