随笔分类 - Leetcode
Leetcode: Smallest Rectangle Enclosing Black Pixels
摘要:An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one bla
阅读全文
Leetcode: Longest Increasing Subsequence
摘要:讲解一: 用一个数组来存 the smallest tail of all increasing subsequences with length i+1 in tails[i]. For example, say we have nums = [4,5,6,3], then all the ava
阅读全文
Leetcode: Serialize and Deserialize Binary Tree
摘要:Referring this post: https://leetcode.com/problems/serialize-and-deserialize-binary-tree/discuss/74253/Easy-to-understand-Java-Solution But instead us
阅读全文
Leetcode: Binary Tree Longest Consecutive Sequence
摘要:递归法 复杂度 时间O(n) 空间O(h) 思路 因为要找最长的连续路径,我们在遍历树的时候需要两个信息,一是目前连起来的路径(curLen)有多长(以当前节点为终点),二是目前路径的上一个节点的值(preVal)。 每一次递归,先检查当前节点是否比上一节点大1, 是,curLen++, 否curL
阅读全文
Leetcode: Best Meeting Point
摘要:A group of two or more people wants to meet and minimize the total travel distance. You are given a 2D grid of values 0 or 1, where each 1 marks the h...
阅读全文
Leetcode: Find Median from Data Stream
摘要:最大最小堆 复杂度 时间 O(logN) insert, O(1) query, 空间 O(N) 思路 维护一个最大堆,一个最小堆。最大堆存的是到目前为止较小的那一半数,最小堆存的是到目前为止较大的那一半数,这样中位数只有可能是堆顶或者堆顶两个数的均值。而维护两个堆的技巧在于判断堆顶数和新来的数的大
阅读全文
Leetcode: Flip Game II
摘要:You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take tu
阅读全文
Leetcode: Flip Game
摘要:You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take tu...
阅读全文
Leetcode: Nim Game
摘要:You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 ston...
阅读全文
Leetcode: Word Pattern II
摘要:Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter...
阅读全文
Leetcode: Word Pattern
摘要:Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a lette
阅读全文
Leetcode: Game of Life
摘要:编解码法 复杂度 时间 O(NN) 空间 O(1) 思路 最简单的方法是再建一个矩阵保存,不过当inplace解时,如果我们直接根据每个点周围的存活数量来修改当前值,由于矩阵是顺序遍历的,这样会影响到下一个点的计算。如何在修改值的同时又保证下一个点的计算不会被影响呢?实际上我们只要将值稍作编码就行了
阅读全文
Leetcode: Alien Dictionary && Summary: Topological Sort
摘要:There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of words from the...
阅读全文
Leetcode: Unique Word Abbreviation
摘要:An abbreviation of a word follows the form . Below are some examples of word abbreviations:a) it --> it (no abbreviation) ...
阅读全文
Leetcode: Find the Duplicate Number
摘要:参考了这个总结,非常详细:http://segmentfault.com/a/1190000003817671 最好方法: 映射找环法 复杂度 时间 O(N) 空间 O(1) 思路 假设数组中没有重复,那我们可以做到这么一点,就是将数组的下标和1到n每一个数一对一的映射起来。比如数组是213,则映射
阅读全文
Leetcode: Walls and Gates
摘要:Push all gates into queue first. Then for each gate update its neighbor cells who is empty and push them to the queue. Repeating above steps until the
阅读全文
Leetcode: Move Zeroes
摘要:第二遍做法: 双指针压缩法:O(N), 空间:O(1) 实际上就是将所有的非0数向前尽可能的压缩,最后把没压缩的那部分全置0就行了。比如103040,先压缩成134,剩余的3为全置为0。过程中需要一个指针记录压缩到的位置。 Q:如果要把所有的0放在前面而不是后面呢?A:同样的解题思路,但是是从后向前
阅读全文
Leetcode: Peeking Iterator
摘要:My thinking is: use a double cache: long cache, when no number is cached, cache = Long.MIN_VALUE. When i call next(), it will firstly check if there's
阅读全文
Leetcode: Inorder Successor in BST
摘要:网上看到更好的方法:https://leetcode.com/discuss/77805/java-5ms-short-code-with-explanations The idea is to compare root's value with p's value if root is not n
阅读全文
浙公网安备 33010602011771号