Cattle off network common algorithm thinking (six) binary search

Common scenarios
ordered sequence to find a number
does not have to only be applied (subject courseware) in the useful part

Investigation point:
Investigation of the boundary conditions of
the condition of the presence or absence of duplicate values are different

Reminded:
MID = (left + right) / 2 possible overflow when left + right, preferably into mid = left + (right-left ) / 2

Case number one

Given unordered array ARR, any two adjacent elements is known, will not be repeated values, a local minimum return any value
local minimum: If arr [0] <arr [1 ], then the location is a local minimum of 0 position. If arr [N-1] is smaller than arr [N-2], then the local minimum position is a position N01.
If not, i is not the value of the leftmost or rightmost sides, as long as i is less than the left and right sides, then i is a local minimum position.
Thinking:
can be binary search to achieve O (logN)
1, a length of 0, direct -1
2, the length 1, directly back to 0
. 3, a length greater than 1, we say about nodes if they meet directly returned, if not, to prove 0> 1, n-1> n- 2, to the middle of the entire array of downward trend by
looking directly at the mid, mid, such as compliance, mid is returned, otherwise, if the mid <mid-1, mid> mid + 1, the front half downward trend of the first half of the binary search, the mid> Similarly to the case of mid-1

Case II

Given an ordered array arr, at a given integer num, please find the leftmost position num the number that appears in the arr.
Ideas:
setting an index result res = -1, when the index recorded find num, similar to the same binary search topic, additional conditions, when searching to find num does not directly stop, but continue half, until the last number

Case III

Given an ordered array arr cycle, returns the minimum value of arr.
Refers to an ordered array of loop of any length into the left to the right, into the left to the right of any length, 12345,51234 cycle are considered ordered
ideas:
1, if arr [L] <arr [R ], direct proof arrays are ordered, direct return arr [L], the minimum is
2, otherwise, have proved array portion of the cycle, check dichotomy mid, if arr [L]> arr [M ] is the minimum value will appear only on LM (because the LM is recycled portion)
3, otherwise, only the minimum value in the MR
. 4, if a [L]> = a [ R], a [L] <= a [M], a [M] <= [R] ==> 222 212 222, then use traversal methods

Case Four

Given an ordered array of non-repetition, find the left-most position to meet a [i] == i conditions. If the number of the positions are not met all of -1 is returned.
Ideas:
1, when a [0]> N-1 , directly returns -1
2, if a [N-1] <0 , the direct return -1
3 a [M]> M. is, look left
4, a [M] <M then, the right to see
5, a [M] == M , because the left looking for, so to find the 0-M

Case 5

Given the first node binary tree head, returns the number of nodes in the tree, if the complete binary tree of nodes is N, traversing complexity is O (N), the optimal solution complexity O (logH²), H is the height
ideas :
because the nature of a complete binary tree, each layer increases from the left to the right node
1, to find the left-most complete binary tree node - "to obtain binary tree height
2, to find the left-most node the first node in the right subtree, if reached the last layer, the prove the first node left subtree of the full binary tree, directly calculated by multiplying the number of left sub-tree nodes, according to the number of recursive step 2 seeking right subtree node
3, and the most left node if the first node has failed to reach the subtree the last layer, the first node to prove the right subtree full binary (one less than the left subtree), calculated as the number of nodes in the right subtree. Step 2 seeking the number of recursive left sub-tree node
4, together with the head node

Six cases

How to get a faster way to N-th power of the integer K, if the obtained result of multiplying two integers of the time complexity is O (1), to give the N-th power of an integer k during implementation complexity O (logN)
Thinking :
Common practice: K times by 1 N-
optimal solution:
provided 75 10 ^
1, disassembled into a binary number 1001011 (7 digit)
2, 10 n-th power demand by doubling manner, Total 7 , 10 1, 10 2, 10 4, 10 8, 16 10, 32 10, 64 10
multiplies 3, the number of binary digits 1 and step 2 , 1 64 + 0 10 + 10 32 + 0 16 10 + 1 8 10 + 0 4 + 1 10 1 10 2 + 1 10 *

发布了11 篇原创文章 · 获赞 14 · 访问量 246

Guess you like

Origin blog.csdn.net/weixin_44303896/article/details/104045424