本文共 2155 字,大约阅读时间需要 7 分钟。
1. how to calculate middle - arithmetic progression(mean) M-S=T-M =>2M=S+T
2. while-loop or recursion: recursion will use system stack space, which should be avoided for simple problem. Recursion should be used for complex algorithm explanation.
3. when the while exit: start + 1 < end; which will exit the loop when there's only two or one elements left to divide.
4. avoid int overflow: start + (end-start)/2; python do not have this issue.
5. how to handle duplicated values, when finding first/last position of the target
end = middle (finding first position) ; start = middle(finding last position)
6. how to implement it with recursion.
when the recursive occurred: start+1 < end
base case: start + 1 >= end
Properties of Recursion
All recursive solutions must satisfy three rules or properties:
1. A recursive solution must contain a base case. 2. A recursive solution must contain a recursive case. 3. A recursive solution must make progress toward the base case.
The base case is the terminating case and represents the smallest subdivision ofthe problem. It signals the end of the virtual loop or recursive calls.
Adjective:
"had adjacent rooms"
"in the next room" "the person sitting next to me" "our rooms were side by side""Rhode Island has two bordering states; Massachusetts and Conncecticut"
"the side of Germany conterminous with France" "Utah and the contiguous state of Idaho" "neighboring cities""lands adjacent to the mountains"
"New York and adjacent cities"转载地址:http://tihbi.baihongyu.com/