2025年6月1日 星期日

34. Find First and Last Position of Element in Sorted Array

 34. Find First and Last Position of Element in Sorted Array

難度: Medium
類型: Array, Binary Search
CPP程式下載: 34.cpp

前情題要:
在一個由小到大排序的陣列中, 尋找與target值相同的起始和最終位置。









思考方式:

一樣用 binary search 去找, 找到之後再用 binary search 去找左邊的起始點和右邊的起始點。


複雜度思考:

Time Complexity: O( log2(N)*2 ) 

Space Complexity: O( x )

結果:

Runtime: 0 ms, Beats: 100%

Memory: 10.60 MB, Beats: 17.13%