The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
- 2月 01 週四 201812:44
Leetcode DP 337 @ Java
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night.
- 1月 30 週二 201809:52
Leetcode Heap 23 @ Java
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
- 1月 30 週二 201800:51
Leetcode BinaryTree 116, 117 @ Java
Given a binary tree
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
}
Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
- You may only use constant extra space.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1
/ \
2 3
/ \ / \
4 5 6 7
After calling your function, the tree should look like:
1 -> NULL
/ \
2 -> 3 -> NULL
/ \ / \
4->5->6->7 -> NULL
注意這題的需求
1. use only extra constant space ----> cann't use resursive. use iterative to do the recursive work
2.perfect binary tree --> each nonleaf node has two child
這題基本上就是一個node做好兩件事情,
1. 把自己的 left child next 跟 right child 接好
2.如果自己有next,就把right child next 也跟 自己的next.left接好
然後一層一層做就可以了
- 1月 25 週四 201813:07
Leetcode Stack 341,636 @ Java
Given a nested list of integers, implement an iterator to flatten it.
Each element is either an integer, or a list -- whose elements may also be integers or other lists.
Example 1:
Given the list [[1,1],2,[1,1]],
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
Example 2:
Given the list [1,[4,[6]]],
By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
這一題要注意的地方是 NestedInteger 要在 hasNext() 裡面展開,而不是在next()
因為如果展開後沒有integer 要return false
ex.[ [ ] ] 的測資
如果是在next 裡面展開就會錯誤,因為實際上根本沒有可以return的 integer
- 1月 25 週四 201811:16
Leetcode String 44 @ Java
444. Wildcard Matching
- 1月 24 週三 201802:22
Leetcode Array 621 @ Java
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.
- 1月 10 週三 201808:48
Leetcode 56 @ Java
Given a collection of intervals, merge all overlapping intervals.
- 1月 08 週一 201808:35
Leetcode 761 @ Java
Special binary strings are binary strings with the following two properties:
- 1月 07 週日 201808:13
Leetcode 139, 238 @ Java
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.
- 1月 05 週五 201809:59
Leetcode 236, 43 @ Java
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4
For example, the lowest common ancestor (LCA) of nodes
5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.這題其實當初我想得太複雜了.....(注意!其實很多題目都是這樣,看清楚題目給的條件,有時候題目加上限制後會變得非常簡單)
如果今天是要找不知道存不存在的兩個node 那這個解法一定不行,但是這題因為是確定存在樹上的兩點node,所以就變得非常簡單
只要思考這兩個node會在樹上存在的可能組合就能找出這組的答案了
一種就是分別在左右subtree,一種就是都在同一個subtree,
左右的情形就是都存在時,那個node就是root了,而當在同一個subtree時,就是第一個相等的那個node開始return就好
