Template & Tips
binary search ๆฆๅฟตๆฏ, ่ฆๆพๅทฆๅ้จ้ๆฏๅณๅ้จ๏ผ
ไธๅๆณ use left <= left
ไบๅๆณ use left < left
1. ๆพๆจก็ณๅผ first or last occurrence, ไบๅๆณ
see leetcode 34
2. ๆพ็ขบๅๅผ, ไธๅๆณ, template,
when do you use while (start < end) , when do you use while (start <= end) ?
You use while (start <= end)
if you are returning the match from inside the loop.
You use while (start < end)
if you want to exit out of the loop first, and then use the result of start
or end
to return the match.
ๅฏไปฅๅ่้็ฏ็ๆ่ทฏ๏ผ
3. ๆพๆ่ฟไผผๅผ (closet to ...)
find peak (162)
Rotated
Mini in Rotated
ไบๅๆณ
ไบๅๆฅๆพ tips
decrease and conquer template
ๅๅง binary search ๅฏซๆณๅๆไธ้จๅ
ๅฆไธ็จฎๅๆณ
decrease and conquer, ๆๆ้ค mid ๆ่, ๅ็บๅ
ฉ้จไปฝ
while(left < right) ้ๆจฃๅฏซ้ๅบๆ, left == right, ๆไปฅๅ left or right ้ฝๅฏไปฅ
ๆ นๆ้็ๆถ็ธฎ,ไธญ้ๅผ่ฆ่ๆ ฎ ๅชๆๅ ฉๅๅ ็ด ๆ, [0, 1] => mid = (0+1)/2 = 0, ๆฐธ้ ๅไธๅฐๅณไธญไฝๆธ
ไธๅๆดๅฏไปฅ่งฃๆฑบ
ๆ นๆไป้บผๆๅ็ๆณไธๆฏ่งฃ, ๅฏซif, ifๅฐ else ่ช็ถๅฐ
็ฌฌไบ่งฃ, ๅ ็บๅฏไปฅๆๅ ฅๅจๆๅพ, ๆไปฅๅฏไปฅไธ็จ็นๅฅๅคๆท, ๆไปฅ ๅณ้็ ๅฏไปฅๆนๆ len
็ฌฌไธ่งฃ, ๅ ็บ็ๅฐ็ฎๆจๅ ็ด , ๅฐฑ้ๆ้ก็ฎ่ฆ็, ๆไปฅๅฏไปฅๅคไธๅ == ็ๆไปถๅคๆท็ดๆฅ่ฟๅ็ญๆก
Last updated
Was this helpful?