Given a binary tree, flatten it to a linked list in-place.
- 3月 22 週四 201804:20
Leetcode Tree,Linkedlist 114 @ Java
Given a binary tree, flatten it to a linked list in-place.
- 3月 06 週二 201802:24
Leetcode DP 764 @ Java
In a 2D grid from (0, 0) to (N-1, N-1), every cell contains a 1, except those cells in the given list mines which are 0. What is the largest axis-aligned plus sign of 1s contained in the grid? Return the order of the plus sign. If there is none, return 0.
- 2月 19 週一 201801:26
Leetcode contest-2/17- BFS 785 @ Java
Given a graph, return true if and only if it is bipartite.
- 2月 10 週六 201803:38
Leetcode Math 670 @Java
Given a non-negative integer, you could swap two digits at most once to get the maximum valued number. Return the maximum valued number you could get.
Example 1:
Input: 2736
Output: 7236
Explanation: Swap the number 2 and the number 7.
Example 2:
Input: 9973
Output: 9973
Explanation: No swap.
Note:
- The given number is in the range [0, 108]
這邊的思維是loop from MSB到LSB,然後每位要用往後找是否有比自己更大的
(不能用往前看,找比自己小的最大位,因為無法確定目前的數字換完就是最大的
ex. 98368 --> 98863,如果用往前找小的,會變成98638,但是其實最佳的換法是8跟3 換)
所以要用小的數往後找最大值來看(因為9一定會讓值是最大,不管在哪位)
一但找到就可以swap 後return
- 2月 09 週五 201801:37
Leetcode BFS 127 @ Java
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- 2月 06 週二 201803:55
Leetcode UnionFind 261 @Java
Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
- 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 週二 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月 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.
