
Leetcode
RongjianShaw
这个作者很懒,什么都没留下…
展开
-
Trie 前缀树 Python实现
题目描述 字典树又称为前缀树或者Trie树,是处理字符串常用的数据结构。假设组成所有单词的字符仅是‘a’~‘z’,请实现字典树的结构,并包含以下四个主要的功能。void insert(String word):添加word,可重复添加;void delete(String word):删除word,如果word添加过多次,仅删除一次;boolean search(String word):查询word是否在字典树中出现过(完整的出现过,前缀式不算);int prefixNumber(String pre):原创 2021-01-31 13:05:09 · 457 阅读 · 0 评论 -
寻找两个有序数组中的第K个数
方法一:双指针 两个指针各指在两个数组的开头,谁小移谁,移动k次就能找到。时间复杂度为O(k) 方法二:分治法 解释在代码中 def FindKthElm(a, b, k): if len(a) == 0: return b[k-1] if len(b) == 0: return a[k-1] a_mid = len(a) // 2 b_mid = len(b) // 2 half_len = a_mid + b_mid +原创 2021-01-30 13:43:27 · 517 阅读 · 0 评论 -
LRU 双向链表+哈希表 python
通过双向链表和哈希表的结合实现LRU class Node: def __init__(self, key = 0, value=0): self.key = key self.value = value self.next = None self.pre = None class Solution: def __init__(self, max_size = 0): self.dumm原创 2021-01-24 11:11:56 · 252 阅读 · 0 评论 -
对青蛙跳台阶的个人理解
预备知识 斐波那契数列 斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:1、1、2、3、5、8、13、21、34、……在数学上,斐波那契数列以如下被以递归的方法定义:F(1)=1,F(2)=1, F(n)=F(n-1)+F(n-2)(n>=2,n∈N*) 通俗来讲当n>=2,该元素为前两位的数字之和,依次往后 问题: 一只青蛙要跳上n层高的台阶,一次能跳一阶,也可以跳2阶,请问这只青蛙跳上n层高的台阶有多少种跳法? 基本思路 设青蛙跳上n级台阶有f(n原创 2020-05-20 12:59:43 · 393 阅读 · 2 评论 -
Leetcode Insert Intervals
Ex.2: Insert Intervals Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to the...原创 2020-05-06 00:53:04 · 171 阅读 · 0 评论 -
Leetcode Merge Intervals 合并区间
Ex.1: Merge Intervals Given a collection of intervals, merge all overlapping intervals. Solution: 分三种情况讨论 两不相交,这个情况选择跳过。比如[1,2],[3,4] 相交,通过前一个的end和后一个的start比较,符合条件则合并。如,[1,3],[2,4] -> [1,4] 包含,先判...原创 2020-05-06 00:47:24 · 232 阅读 · 0 评论 -
Leetcode 矩阵搜索
Ex.11: 矩阵搜索 在一个N*M的矩阵里,每一行都是排好序的,每一列也都是排好序的,请设计一个算法在矩阵中查找一个数。 Solution: 题目的条件是每行每列都是升序的,那么可以利用这一点。 那么有没有办法可以让程序自己去寻找一个路径?也就是说对于路径走到任何一个数,如果给定条件的话,只有一个方向走 矩阵中,位置有2,3,4三种可能的方向个数,比如四个角的地方有2个方向可以走,除去外圈的所有...原创 2020-05-06 00:43:19 · 400 阅读 · 0 评论 -
Leetcode sqrt 平方根
sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-negative integer. ''' Solution: 二分法,判断要求:mid²和x的关系 ''' def sqrt(x): if x <= 0: r...原创 2020-05-06 00:41:37 · 165 阅读 · 0 评论