
LeetCode
文章平均质量分 59
BrownWong
None
展开
-
求职笔试题
【代码】求职笔试题。原创 2025-03-29 22:01:07 · 197 阅读 · 0 评论 -
(LeetCode)字符串
1. 实现 Trie (前缀树)208. implement-trie-prefix-treeclass Trie(object): def __init__(self): """ Initialize your data structure here. 用dict嵌套构造字典树 如果一个节点包含空的子节点, 说明其可以作为终止符 """ self.tree = {} def ins原创 2020-06-21 16:44:20 · 264 阅读 · 0 评论 -
(LeetCode)排序
1. 排序链表148. sort-list# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def sortList(self, head): """ :type head: Lis原创 2020-06-21 16:41:13 · 284 阅读 · 0 评论 -
(LeetCode)位运算
1. 汉明距离461. hamming-distanceclass Solution(object): def hammingDistance(self, x, y): """ :type x: int :type y: int :rtype: int 思路 先异或, 再使用bin函数统计1的个数 bin(xor).count('1') 先原创 2020-06-21 16:36:02 · 320 阅读 · 0 评论 -
(LeetCode)全排列, 回溯法
1. 子集78. subsetsclass Solution(object): def subsets(self, nums): """ :type nums: List[int] :rtype: List[List[int]] 方法 回溯法, 循环内递归, 即DFS https://pic.leetcode-cn.com/a4f4acf7177b6a1131b1ef5da97c1原创 2020-06-21 16:31:37 · 396 阅读 · 0 评论 -
(LeetCode)栈
1. 最小栈155. min-stackclass MinStack(object): def __init__(self): """ initialize your data structure here. 分析 单纯地用数组实现一个栈很容易,但要实现常数时间检索到最小值就麻烦了; """ self.data = [] self.mins = [] s原创 2020-06-21 16:25:37 · 240 阅读 · 0 评论 -
(LeetCode)双指针
1. 移动零283. move-zeroesclass Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: None Do not return anything, modify nums in-place instead. 聪明解法 1. 两个指针,i指针迭代,j指针表示(从左往右)非零元素个数的原创 2020-06-21 16:17:09 · 497 阅读 · 0 评论 -
(LeetCode)哈希表
1. 两数之和1. two-sumclass Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] 笨办法 暴力求解 聪明办法 利用dict存储 "原创 2020-06-21 16:05:25 · 176 阅读 · 0 评论 -
(LeetCode)动态规划
1. 买卖股票的最佳时机121. best-time-to-buy-and-sell-stockclass Solution(object): def maxProfit(self, prices): """ :type prices: List[int] :rtype: int 笨办法 暴力求解,两重循环 聪明法 动态规划,前n天最大利润=max原创 2020-06-21 16:01:20 · 337 阅读 · 0 评论 -
(LeetCode)数组
1. 多数元素169. majority-elementclass Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: int 1. Counter return Counter(nums).most_common(1)[0][0] 2. 统计频次,取最大 **原创 2020-06-21 15:50:30 · 217 阅读 · 1 评论 -
(LeetCode)链表
1. 反转链表206. reverse-linked-list# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# self.val = x# self.next = Noneclass Solution(object): def reverseList(self, head): """ :t原创 2020-06-21 14:41:59 · 198 阅读 · 0 评论 -
(LeetCode)二叉树
1. 合并二叉树617. merge-two-binary-trees# Definition for a binary tree node.# class TreeNode(object):# def __init__(self, x):# self.val = x# self.left = None# self.right = Noneclass Solution(object): def mergeTrees(sel原创 2020-06-21 14:27:28 · 2834 阅读 · 3 评论