Linkage
https://leetcode.com/problems/search-for-a-range/description/
Code
1 | class Solution { |
Why don't you come to your senses?
https://leetcode.com/problems/search-for-a-range/description/
1 | class Solution { |
https://leetcode.com/problems/next-permutation/description/
1 | class Solution { |
The key idea for my O(n) algorithm is keeping two stacks named position_stack and height_stack while traverse. The definition of stacks is defined in following:(Pay attention the size of the two stacks should keep consistent)
Let the original height array be heights
, A equal position_stack[i]
, B equal height_stack[i]
. This notation means the max height of the qualified rectangle starting at index A(Ending at the current cursor index
) in heights
is B.
The stack has a little trick in my implementation. I used a sentinel node which is less then all the normal heights and indexed at -1 as the first item in the stacks, to simplify the code when handling the corner case.
1 | class Solution: |
https://leetcode.com/problems/permutations/description/
1 | class Solution: |
https://leetcode.com/problems/permutations-ii/description/
In this implementation, we need the preprocessing to the original array, which means we should count the number of each elements.
1 | import java.util.HashMap; |
The brutal-force way to calculate all the combination.
https://leetcode.com/problems/combinations/description/
1 | class Solution: |
https://leetcode.com/problems/search-in-rotated-sorted-array/description/
In this situation, we need another item for comparison when we determine which side to do further search. In my code, we set the first item of the array as the compliment to help us compare. To be more specific, the code show the two cases which composes of all the situation.
1 | class Solution: |
We should utilize the dynamic perogramming, the transfer equation is, suppose result[i] is the number of decoding ways ending at s[i]
result[i] = result[i-1] + result[i-2] (If both s[i-1~ i] and s[i] is valid)
result[i] = result[i-1] (If only s[i] is valid)
Pay attention to the corner case!!!!
1 | class Solution: |
1.This is a naive implementation of Trie, which do not support the delete function, furthermore, the long string which can match multiple words is also not supported.
2.The key and value of a map must be Object, the basic type like int and char is not supported.
1 | import java.util.HashMap; |
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
Example 1:
1 | Input: [3,2,1,5,6,4] and k = 2 |
Example 2:
1 | Input: [3,2,3,1,2,4,5,5,6] and k = 4 |
Note:
You may assume k is always valid, 1 ≤ k ≤ array’s length.
1 | class Solution(object): |
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
1 | nums1 = [1, 3] |
Example 2:
1 | nums1 = [1, 2] |
1 | class Solution(object): |
####
How to prove the time scale is log(m+n)