Python
zzamx
这个作者很懒,什么都没留下…
展开
-
【leetcode】快速排序(快排)(python)
【代码】【leetcode】快速排序(快排)(python)原创 2023-07-11 22:46:21 · 389 阅读 · 0 评论 -
【leetcode】61. 旋转链表 (python)
【代码】【leetcode】61. 旋转链表 (python)原创 2023-07-11 19:30:39 · 314 阅读 · 0 评论 -
【leetcode】面试题 02.01. 移除重复节点 (python + 链表)
【代码】【leetcode】面试题 02.01. 移除重复节点 (python + 链表)原创 2023-07-11 18:30:31 · 305 阅读 · 0 评论 -
【leetcode】15.三数之和(python+转为两数之和+去重)
【代码】【leetcode】15.三数之和(python+转为两数之和+去重)原创 2023-07-08 09:42:16 · 384 阅读 · 0 评论 -
【matplotlib】plt 动态多次绘图,图片被覆盖问题——清除figure、axes
在同一个函数中未subplot进行多张图的绘制,发现总是显示一样的图,原来是后边的和前边的图叠加了,清除之前的figure就行了。【matplotlib】 之 清理、清除 axes 和 figure (plt.cla、plt.clf、plt.close)原创 2023-02-22 15:58:57 · 3796 阅读 · 0 评论 -
【python】TypeError: unhashable type: ‘list‘ 解决方法——简单示例
【python】TypeError: unhashable type: 'list' 解决方法——简单示例。原创 2022-11-19 10:50:08 · 1525 阅读 · 0 评论 -
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(python,暴力,双指针)
【leetcode】剑指 Offer II 007. 数组中和为 0 的三个数(python,暴力,双指针)原创 2022-11-19 10:32:32 · 289 阅读 · 0 评论 -
【leetcode】剑指 Offer II 009. 乘积小于 K 的子数组(python)
乘积小于K的子数组,滑动窗口原创 2022-11-16 10:38:07 · 216 阅读 · 0 评论 -
【leetcode】剑指 Offer 26. 树的子结构(python)
剑指 Offer 26. 树的子结构原创 2022-06-17 15:50:53 · 62 阅读 · 0 评论 -
【leetcode】剑指 Offer 04. 二维数组中的查找(python)
剑指 Offer 04. 二维数组中的查找原创 2022-06-15 15:59:55 · 266 阅读 · 0 评论 -
【leetcode】剑指 Offer 30. 包含min函数的栈 (python)
剑指 Offer 30. 包含min函数的栈原创 2022-06-15 14:21:12 · 77 阅读 · 0 评论 -
【leetcode】5. 最长回文子串(python,动态规划)
思路承接上一篇文章【leetcode】647. 回文子串(python,动态规划法;中心拓展法;暴力破解)原创 2022-06-01 09:35:14 · 92 阅读 · 0 评论 -
【leetcode】647. 回文子串(python,动态规划法;中心拓展法;暴力破解)
方法一:动态规划class Solution: def countSubstrings(self, s: str) -> int: cnt = 0 n = len(s) dp = [[False for i in range(n)] for j in range(n)] for i in range(n - 1, -1, -1): for j in range(i, n): .原创 2022-05-31 13:36:56 · 176 阅读 · 0 评论 -
【leetcode】136. 只出现一次的数字(python)位运算(异或运算)
方法一,位运算(异或运算),空间复杂度O(1)class Solution: def singleNumber(self, nums): return reduce(lambda x, y: x ^ y, nums)方法二:统计数量,空间复杂度O(n)import collectionsclass Solution: def singleNumber(self, nums): dic = collections.Counter(nums) .原创 2022-05-31 10:14:54 · 93 阅读 · 0 评论 -
【leetcode】409. 最长回文串(python)
看清题目,没让你构造输出具体的回文串字符,只是让统计长度。import collectionsclass Solution: def longestPalindrome(self, s: str) -> int: dic = collections.Counter(s) ans = 0 flag = 1 for num in dic.values(): ans += num // 2 * 2 .原创 2022-05-22 16:12:41 · 108 阅读 · 0 评论 -
【leetcode】剑指 Offer II 019. 最多删除一个字符得到回文(python)
思路: 双指针note: 只要左边或者右边删除一个字符后是回文即可。class Solution: def isPalindrome(self, s, low, high): i, j = low, high while i < j: if s[i] == s[j]: i += 1 j -= 1 else: re.原创 2022-05-22 15:37:49 · 271 阅读 · 0 评论 -
【leetcode】125. 验证回文串(python)
写法一class Solution: def isPalindrome(self, s: str) -> bool: res = "".join(ch.lower() for ch in s if ch.isalnum()) return res == res[::-1]写法二:class Solution: def isPalindrome(self, s: str) -> bool: lst = [] .原创 2022-05-21 14:03:26 · 122 阅读 · 0 评论 -
【python】9. 回文数(python)
方法一:将数字转换为字符串class Solution: def isPalindrome(self, x: int) -> bool: x = str(x) j = len(x) - 1 for i in range(len(x)): if x[i] != x[j]: return False j -= 1 return True方法二:反转.原创 2022-05-21 13:29:56 · 381 阅读 · 0 评论 -
【leetcode】572. 另一棵树的子树(python)
思路:先整体遍历一遍root,在root上找到与subRoot的根节点相同的节点值,再用递归(isSame函数)依次判断root的子树和subRoot的节点值和结构是否一致。注意测试用例:[1,1][1] 即,可能一棵树上有多个相同的节点值,此时只要root的左孩子1和subRoot匹配,即可返回True# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=..原创 2022-05-20 21:37:00 · 279 阅读 · 0 评论 -
【leetcode】199. 二叉树的右视图(python)
思路:层次遍历,每层的最后一个节点即为该层深度所看到的右侧节点。层次遍历# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def ..原创 2022-05-17 21:59:39 · 271 阅读 · 0 评论 -
【leetcode】965. 单值二叉树(python)
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def isUnivalTree(self, root: TreeNode) -&g..原创 2022-05-15 20:19:08 · 130 阅读 · 0 评论 -
【leetcode】剑指 Offer 54. 二叉搜索树的第k大节点(python)
思路:先序遍历,把节点值存储在lst中,直接输出第k大的节点值# Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution: def kthLargest(self, root: TreeNode, k: .原创 2022-05-15 20:17:34 · 163 阅读 · 0 评论 -
【leetcode】102. 二叉树的层序遍历(python)
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def levelOrder(self, root: TreeNode) -> .原创 2022-05-13 21:24:07 · 283 阅读 · 0 评论 -
【leetcode】559. N 叉树的最大深度(python)DFS + BFS
方法一:广度优先搜索(BFS)"""# Definition for a Node.class Node: def __init__(self, val=None, children=None): self.val = val self.children = children"""class Solution: def maxDepth(self, root: 'Node') -> int: if not root: ..原创 2022-05-13 20:51:35 · 145 阅读 · 0 评论 -
【leetcode】110. 平衡二叉树(python)自顶向下的递归 + 自底向上的递归
方法一:自顶向下的递归# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def isBalanced(self, root: Tre..原创 2022-05-12 18:16:36 · 335 阅读 · 0 评论 -
【leetcode】404. 左叶子之和 (python)DFS + BFS
方法一:DFS(深度优先搜索)# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def sumOfLeftLeaves(self, r.原创 2022-05-11 15:20:35 · 144 阅读 · 0 评论 -
【leetcode】100. 相同的树(python)深度优先 + 广度优先
方法一:深度优先搜索(递归)# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclass Solution: def isSameTree(self, p: Tr...原创 2022-05-08 14:40:00 · 284 阅读 · 0 评论 -
【leetcode】82. 删除排序链表中的重复元素 II(python)
注意:head也有可能被删除,所以设置个哑变量dummy指向head,统一操作。# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def deleteDuplicates(self, head: ListNode)..原创 2022-05-06 15:47:47 · 597 阅读 · 0 评论 -
【leetcode】328. 奇偶链表(python)
写法一# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def oddEvenList(self, head: Optional[ListNode]) -> Optional[ListNode]: ..原创 2022-05-06 15:40:58 · 703 阅读 · 0 评论 -
【leetcode】61. 旋转链表(python)
细节:若 k == 0 (包括取模之后k = 0)时,直接返回原链表即可。注意末尾节点指向None。# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def rotateRight(self, head: ..原创 2022-05-06 15:38:18 · 505 阅读 · 0 评论 -
【leetcode】92. 反转链表 II(python)
思路详见注释# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def reverse(self, head): pre = head cur = head.next .原创 2022-05-05 13:39:26 · 405 阅读 · 0 评论 -
【leetcode】445. 两数相加 II(python)
方法:链表反转后相加# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def reverse(self, head): # 链表反转 pre = head cur = head.n.原创 2022-05-04 16:03:36 · 647 阅读 · 0 评论 -
【leetcode】剑指 Offer II 027. 回文链表(python)
方法一:借助数组,空间复杂度O(n)# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def isPalindrome(self, head: ListNode) -> bool: nums =.原创 2022-05-03 14:36:35 · 810 阅读 · 0 评论 -
【leetcode】剑指 Offer II 024. 反转链表(python)
方法一:迭代# Definition for singly-linked list.# class ListNode:# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution: def reverseList(self, head: ListNode) -> ListNode: if not head or..原创 2022-05-03 13:41:28 · 540 阅读 · 0 评论 -
【leetcode】剑指 Offer 22. 链表中倒数第k个节点(python)
小记:一开始用的元素值作为条件判断,但是忽略了链表中可能有重复元素。注意:测试用例:[4,5,4] 1方法一:双指针(快慢指针)# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def getKthFromEnd(self, head: .原创 2022-05-01 15:54:11 · 286 阅读 · 0 评论 -
【leetcode】剑指 Offer 25. 合并两个排序的链表(python)
写法一:# Definition for singly-linked list.# class ListNode:# def __init__(self, x):# self.val = x# self.next = Noneclass Solution: def mergeTwoLists(self, l1: ListNode, l2: ListNode) -> ListNode: # 已AC if.原创 2022-05-01 15:09:42 · 728 阅读 · 0 评论 -
【leetcode】剑指 Offer 35. 复杂链表的复制(python)
思路:哈希表利用哈希表的查询特点,考虑构建 原链表节点 和 新链表对应节点 的键值对映射关系,再遍历构建新链表各节点的 next 和 random 引用指向即可。详细请看leetcode题解,本文仅个人take notes"""# Definition for a Node.class Node: def __init__(self, x: int, next: 'Node' = None, random: 'Node' = None): self.val = int(x.原创 2022-04-30 20:20:41 · 428 阅读 · 0 评论 -
【leetcode】203. 移除链表元素(python)
写法一:不新建头结点# Definition for singly-linked list.# class ListNode(object):# def __init__(self, val=0, next=None):# self.val = val# self.next = nextclass Solution(object): def removeElements(self, head, val): """ .原创 2022-04-26 14:16:21 · 999 阅读 · 0 评论 -
【leetcode】387. 字符串中的第一个唯一字符(python)
写法一:用Counterimport collectionsclass Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ s_dic = collections.Counter(s) for i, ch in enumerate(s): if s_dic[ch] == .原创 2022-04-26 13:33:16 · 255 阅读 · 0 评论 -
【leetcode】383. 赎金信(python)
import collectionsclass Solution(object): def canConstruct(self, ransomNote, magazine): """ :type ransomNote: str :type magazine: str :rtype: bool """ mag_dic = collections.Counter(magazine) for.原创 2022-04-26 13:30:07 · 352 阅读 · 0 评论