随笔分类 - Leetcode
Leetcode: Matchsticks to Square && Grammar: reverse an primative array
摘要:DFS, my solution is to fill each edge of the square one by one. DFS to construct the 1st, then 2nd, then 3rd, then 4th. For each edge I scan all the m
阅读全文
Leetcode: Serialize and Deserialize BST
摘要:So the first question is: what is the difference between this and #297? This here is BST, however, in #297, it's BT. "The encoded string should be as
阅读全文
Leetcode: Circular Array Loop
摘要:注意The loop must be "forward" or "backward'. 所以这就是为什么[-2, 1, -1, -2, -2]是false的原因 Just think it as finding a loop in Linkedlist, except that loops with
阅读全文
Leetcode: Sequence Reconstruction
摘要:Topological Sort: This problem is to determine if there's one, and only one sequence to sort a DAG. The method is to check if the queue's size is alwa
阅读全文
Leetcode: Ternary Expression Parser
摘要:My First Solution: Use Stack and String operation, from the back of the string, find the first '?', push the right to stack. Depends on whether the ch
阅读全文
Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
摘要:Backtracking + Trie: referred to https://discuss.leetcode.com/topic/63516/explained-my-java-solution-using-trie-126ms-16-16 A better approach is to ch
阅读全文
Leetcode: Ones and Zeroes
摘要:This is a 0/1 backpacking problem The problem can be interpreted as: What's the max number of str can we pick from strs with limitation of m "0"s and
阅读全文
Leetcode: Heaters
摘要:Binary Search My solution: Be careful in my binary search function, l, r may go out of the range of the array Solution with the highest vote:
阅读全文
Leetcode: Total Hamming Distance
摘要:Example: 0 0 0 0 0 1 0 0 1 1 1 0 0 0 1 0 Total Hamming Distance = (3*1) + (2*2) + (2*2) + (4*0) = 11 so the idea is count the number of 1 and 0 on eac
阅读全文
Leetcode: Valid Word Square
摘要:这题看起来简单但是其实很容易写错 本来想j 不从0开始,而是从 i+1开始检查,可以节省时间,但是一些为Null的情况会使讨论很复杂 比如
阅读全文
Leetcode: Sentence Screen Fitting
摘要:先来一个brute force, 类似Text Adjustment 但是在稍微大一点的case就TLE了,比如: ["a","b","e"] 20000 20000, 花了465ms 所以想想怎么节约时间, 提示是可以DP的,想想怎么复用,refer to: https://discuss.lee
阅读全文
Leetcode: Minimum Unique Word Abbreviation
摘要:Refer to https://discuss.leetcode.com/topic/61799/java-bit-mask-dfs-with-pruning bit mask refer to http://bookshadow.com/weblog/2016/10/02/leetcode-mi
阅读全文
Leetcode: Design Phone Directory
摘要:my HashSet+ ArrayList, 删除的时候把要删的index与末尾对调。get()其实不需要random, 因为anyone is ok HashSet+ Queue网上vote最高的solution,
阅读全文
Leetcode: Valid Word Abbreviation
摘要:Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as "word" contains only the following valid abbreviations: ["word",...
阅读全文
Leetcode: Find Leaves of Binary Tree
摘要:Better Solution: https://discuss.leetcode.com/topic/49194/10-lines-simple-java-solution-using-recursion-with-explanation/2 For this question we need t
阅读全文
Leetcode: Design Hit Counter
摘要:Use Queue Better Solution: can solve follow up, refer to https://discuss.leetcode.com/topic/48758/super-easy-design-o-1-hit-o-s-gethits-no-fancy-data-
阅读全文
Leetcode: Bomb Enemy
摘要:Walk through the matrix. At the start of each non-wall-streak (row-wise or column-wise), count the number of hits in that streak and remember it. For
阅读全文
浙公网安备 33010602011771号