Find All Numbers Disappeared in an Array
Given an array of integers where $1 ≤ a[i] ≤ n$ (n = size of array), some elements appear twice and others appear once.
Find all the elements of $[1, n]$ inclusive that do not appear in this array.
Could you do it without extra space and in $O(n)$ runtime? You may assume the returned list does not count as extra space.
Example:1
2
3
4
5Input:
[4,3,2,7,8,2,3,1]
Output:
[5,6]
1 | ; |
Find Bottom Left Tree Value
Given a binary tree, find the leftmost value in the last row of the tree.
Example 1:1
2
3
4
5
6
7
8Input:
2
/ \
1 3
Output:
1
Example 2:1
2
3
4
5
6
7
8
9
10
11
12Input:
1
/ \
2 3
/ / \
4 5 6
/
7
Output:
7
Note: You may assume the tree (i.e., the given root node) is not NULL.
1 | /** |
Find K Pairs with Smallest Sums
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
Define a pair (u,v) which consists of one element from the first array and one element from the second array.
Find the k pairs (u1,v1),(u2,v2) …(uk,vk) with the smallest sums.
Example 1:1
2
3
4
5
6Given nums1 = [1,7,11], nums2 = [2,4,6], k = 3
Return: [1,2],[1,4],[1,6]
The first 3 pairs are returned from the sequence:
[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
Example 2:1
2
3
4
5
6Given nums1 = [1,1,2], nums2 = [1,2,3], k = 2
Return: [1,1],[1,1]
The first 2 pairs are returned from the sequence:
[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
Example 3:1
2
3
4
5
6Given nums1 = [1,2], nums2 = [3], k = 3
Return: [1,3],[2,3]
All possible pairs are returned from the sequence:
[1,3],[2,3]
1 | /** |
Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree.
Example:1
2
3
4
5
6
7
8
9Input:
1
/ \
3 2
/ \ \
5 3 9
Output: [1, 3, 9]
1 | /** |
Find Median from Data Stream
Median is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle value.
Examples:[2,3,4]
, the median is 3
[2,3]
, the median is (2 + 3) / 2 = 2.5
Design a data structure that supports the following two operations:
- void addNum(int num) - Add a integer number from the data stream to the data structure.
- double findMedian() - Return the median of all elements so far.
For example:1
2
3
4
5addNum(1)
addNum(2)
findMedian() -> 1.5
addNum(3)
findMedian() -> 2
1 | function binarySearch(a, target) { |