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:
- nearest in space or position; immediately adjoining without intervening space;
"had adjacent rooms"
"in the next room"
"the person sitting next to me"
"our rooms were side by side"
- having a common boundary or edge; abutting; touching;
"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"
- near or close to but not necessarily touching;
"lands adjacent to the mountains"
"New York and adjacent cities"
- The house adjacent to ours has been sold.
与我们家邻接的房子已经售出。 - His house is adjacent to mine.
他的寓所与我的相毗连。 - The planes landed on adjacent runways.
这些飞机在毗连的跑道上降落。 - The island of Cuba is adjacent to Florida.
古巴岛接近佛罗里达。 - The hospital is adjacent to a church.
这家医院紧挨着一座教堂。
本文深入探讨了计算算术平均数的方法、循环与递归的区别、循环退出条件、避免整数溢出的技巧、处理重复值的策略以及递归实现的要点。通过实例分析,阐述了递归解决方案必须遵循的三个关键属性:基案例、递归案例及逐步逼近基案例的过程。
2万+

被折叠的 条评论
为什么被折叠?



