@ -2015,23 +2015,45 @@
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.py< / span > < pre > < span > < / span > < code > < a id = "__codelineno-2-1" name = "__codelineno-2-1" href = "#__codelineno-2-1" > < / a > < span class = "k" > def< / span > < span class = "nf" > binary_search_left_edge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > :< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > int< / span > < span class = "p" > ],< / span > < span class = "n" > target< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "nb" > int< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-2" name = "__codelineno-2-2" href = "#__codelineno-2-2" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 二分查找最左一个元素" " " < / span >
< a id = "__codelineno-2-3" name = "__codelineno-2-3" href = "#__codelineno-2-3" > < / a > < span class = "c1" > # 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素< / span >
< a id = "__codelineno-2-4" name = "__codelineno-2-4" href = "#__codelineno-2-4" > < / a > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-2-5" name = "__codelineno-2-5" href = "#__codelineno-2-5" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-6" name = "__codelineno-2-6" href = "#__codelineno-2-6" > < / a > < span class = "n" > m< / span > < span class = "o" > =< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "n" > j< / span > < span class = "p" > )< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "c1" > # 计算中点索引 m< / span >
< a id = "__codelineno-2-7" name = "__codelineno-2-7" href = "#__codelineno-2-7" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-8" name = "__codelineno-2-8" href = "#__codelineno-2-8" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-2-9" name = "__codelineno-2-9" href = "#__codelineno-2-9" > < / a > < span class = "k" > elif< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-10" name = "__codelineno-2-10" href = "#__codelineno-2-10" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-2-11" name = "__codelineno-2-11" href = "#__codelineno-2-11" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-12" name = "__codelineno-2-12" href = "#__codelineno-2-12" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 首个小于 target 的元素在区间 [i, m-1] 中< / span >
< a id = "__codelineno-2-13" name = "__codelineno-2-13" href = "#__codelineno-2-13" > < / a > < span class = "k" > if< / span > < span class = "n" > i< / span > < span class = "o" > ==< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "ow" > or< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-14" name = "__codelineno-2-14" href = "#__codelineno-2-14" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-2-15" name = "__codelineno-2-15" href = "#__codelineno-2-15" > < / a > < span class = "k" > return< / span > < span class = "n" > i< / span >
< a id = "__codelineno-2-3" name = "__codelineno-2-3" href = "#__codelineno-2-3" > < / a > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-2-4" name = "__codelineno-2-4" href = "#__codelineno-2-4" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-5" name = "__codelineno-2-5" href = "#__codelineno-2-5" > < / a > < span class = "n" > m< / span > < span class = "o" > =< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "n" > j< / span > < span class = "p" > )< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "c1" > # 计算中点索引 m< / span >
< a id = "__codelineno-2-6" name = "__codelineno-2-6" href = "#__codelineno-2-6" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-7" name = "__codelineno-2-7" href = "#__codelineno-2-7" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-2-8" name = "__codelineno-2-8" href = "#__codelineno-2-8" > < / a > < span class = "k" > elif< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-9" name = "__codelineno-2-9" href = "#__codelineno-2-9" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-2-10" name = "__codelineno-2-10" href = "#__codelineno-2-10" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-11" name = "__codelineno-2-11" href = "#__codelineno-2-11" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 首个小于 target 的元素在区间 [i, m-1] 中< / span >
< a id = "__codelineno-2-12" name = "__codelineno-2-12" href = "#__codelineno-2-12" > < / a > < span class = "k" > if< / span > < span class = "n" > i< / span > < span class = "o" > ==< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "ow" > or< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-2-13" name = "__codelineno-2-13" href = "#__codelineno-2-13" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-2-14" name = "__codelineno-2-14" href = "#__codelineno-2-14" > < / a > < span class = "k" > return< / span > < span class = "n" > i< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-3-1" name = "__codelineno-3-1" href = "#__codelineno-3-1" > < / a > < span class = "p" > [< / span > < span class = "nx" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "kd" > func< / span > < span class = "p" > ]{< / span > < span class = "nx" > binarySearchLeftEdge< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > binary_search_edge.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-3-1" name = "__codelineno-3-1" href = "#__codelineno-3-1" > < / a > < span class = "cm" > /* 二分查找最左一个元素 */< / span >
< a id = "__codelineno-3-2" name = "__codelineno-3-2" href = "#__codelineno-3-2" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > binarySearchLeftEdge< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "w" > < / span > < span class = "p" > []< / span > < span class = "kt" > int< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-3" name = "__codelineno-3-3" href = "#__codelineno-3-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-3-4" name = "__codelineno-3-4" href = "#__codelineno-3-4" > < / a > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-3-5" name = "__codelineno-3-5" href = "#__codelineno-3-5" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-6" name = "__codelineno-3-6" href = "#__codelineno-3-6" > < / a > < span class = "w" > < / span > < span class = "c1" > // 计算中点索引 m< / span >
< a id = "__codelineno-3-7" name = "__codelineno-3-7" href = "#__codelineno-3-7" > < / a > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > j< / span > < span class = "o" > -< / span > < span class = "nx" > i< / span > < span class = "p" > )< / span > < span class = "o" > /< / span > < span class = "mi" > 2< / span >
< a id = "__codelineno-3-8" name = "__codelineno-3-8" href = "#__codelineno-3-8" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > m< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-9" name = "__codelineno-3-9" href = "#__codelineno-3-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-3-10" name = "__codelineno-3-10" href = "#__codelineno-3-10" > < / a > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-3-11" name = "__codelineno-3-11" href = "#__codelineno-3-11" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > m< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > > < / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-12" name = "__codelineno-3-12" href = "#__codelineno-3-12" > < / a > < span class = "w" > < / span > < span class = "c1" > // target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-3-13" name = "__codelineno-3-13" href = "#__codelineno-3-13" > < / a > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-3-14" name = "__codelineno-3-14" href = "#__codelineno-3-14" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-15" name = "__codelineno-3-15" href = "#__codelineno-3-15" > < / a > < span class = "w" > < / span > < span class = "c1" > // 首个小于 target 的元素在区间 [i, m-1] 中< / span >
< a id = "__codelineno-3-16" name = "__codelineno-3-16" href = "#__codelineno-3-16" > < / a > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-3-17" name = "__codelineno-3-17" href = "#__codelineno-3-17" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-3-18" name = "__codelineno-3-18" href = "#__codelineno-3-18" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-3-19" name = "__codelineno-3-19" href = "#__codelineno-3-19" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > ==< / span > < span class = "w" > < / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > i< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-3-20" name = "__codelineno-3-20" href = "#__codelineno-3-20" > < / a > < span class = "w" > < / span > < span class = "c1" > // 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-3-21" name = "__codelineno-3-21" href = "#__codelineno-3-21" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-3-22" name = "__codelineno-3-22" href = "#__codelineno-3-22" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-3-23" name = "__codelineno-3-23" href = "#__codelineno-3-23" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > i< / span >
< a id = "__codelineno-3-24" name = "__codelineno-3-24" href = "#__codelineno-3-24" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2051,7 +2073,26 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-8-1" name = "__codelineno-8-1" href = "#__codelineno-8-1" > < / a > < span class = "p" > [< / span > < span class = "kd" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "kd" > func< / span > < span class = "p" > ]{< / span > < span class = "n" > binarySearchLeftEdge< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > binary_search_edge.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-8-1" name = "__codelineno-8-1" href = "#__codelineno-8-1" > < / a > < span class = "cm" > /* 二分查找最左一个元素 */< / span >
< a id = "__codelineno-8-2" name = "__codelineno-8-2" href = "#__codelineno-8-2" > < / a > < span class = "kd" > func< / span > < span class = "nf" > binarySearchLeftEdge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > :< / span > < span class = "p" > [< / span > < span class = "nb" > Int< / span > < span class = "p" > ],< / span > < span class = "n" > target< / span > < span class = "p" > :< / span > < span class = "nb" > Int< / span > < span class = "p" > )< / span > < span class = "p" > -> < / span > < span class = "nb" > Int< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-3" name = "__codelineno-8-3" href = "#__codelineno-8-3" > < / a > < span class = "c1" > // 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-8-4" name = "__codelineno-8-4" href = "#__codelineno-8-4" > < / a > < span class = "kd" > var< / span > < span class = "nv" > i< / span > < span class = "p" > =< / span > < span class = "mi" > 0< / span >
< a id = "__codelineno-8-5" name = "__codelineno-8-5" href = "#__codelineno-8-5" > < / a > < span class = "kd" > var< / span > < span class = "nv" > j< / span > < span class = "p" > =< / span > < span class = "n" > nums< / span > < span class = "p" > .< / span > < span class = "bp" > count< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-8-6" name = "__codelineno-8-6" href = "#__codelineno-8-6" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-7" name = "__codelineno-8-7" href = "#__codelineno-8-7" > < / a > < span class = "kd" > let< / span > < span class = "nv" > m< / span > < span class = "p" > =< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "p" > (< / span > < span class = "n" > j< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "p" > )< / span > < span class = "o" > /< / span > < span class = "mi" > 2< / span > < span class = "c1" > // 计算中点索引 m< / span >
< a id = "__codelineno-8-8" name = "__codelineno-8-8" href = "#__codelineno-8-8" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-9" name = "__codelineno-8-9" href = "#__codelineno-8-9" > < / a > < span class = "n" > i< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > // target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-8-10" name = "__codelineno-8-10" href = "#__codelineno-8-10" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-11" name = "__codelineno-8-11" href = "#__codelineno-8-11" > < / a > < span class = "n" > j< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > // target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-8-12" name = "__codelineno-8-12" href = "#__codelineno-8-12" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-13" name = "__codelineno-8-13" href = "#__codelineno-8-13" > < / a > < span class = "n" > j< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > // 首个小于 target 的元素在区间 [i, m-1] 中< / span >
< a id = "__codelineno-8-14" name = "__codelineno-8-14" href = "#__codelineno-8-14" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-8-15" name = "__codelineno-8-15" href = "#__codelineno-8-15" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-8-16" name = "__codelineno-8-16" href = "#__codelineno-8-16" > < / a > < span class = "k" > if< / span > < span class = "n" > i< / span > < span class = "p" > ==< / span > < span class = "n" > nums< / span > < span class = "p" > .< / span > < span class = "bp" > count< / span > < span class = "o" > ||< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > i< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-8-17" name = "__codelineno-8-17" href = "#__codelineno-8-17" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > // 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-8-18" name = "__codelineno-8-18" href = "#__codelineno-8-18" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-8-19" name = "__codelineno-8-19" href = "#__codelineno-8-19" > < / a > < span class = "k" > return< / span > < span class = "n" > i< / span >
< a id = "__codelineno-8-20" name = "__codelineno-8-20" href = "#__codelineno-8-20" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2106,23 +2147,45 @@
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.py< / span > < pre > < span > < / span > < code > < a id = "__codelineno-12-1" name = "__codelineno-12-1" href = "#__codelineno-12-1" > < / a > < span class = "k" > def< / span > < span class = "nf" > binary_search_right_edge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > :< / span > < span class = "nb" > list< / span > < span class = "p" > [< / span > < span class = "nb" > int< / span > < span class = "p" > ],< / span > < span class = "n" > target< / span > < span class = "p" > :< / span > < span class = "nb" > int< / span > < span class = "p" > )< / span > < span class = "o" > -> < / span > < span class = "nb" > int< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-2" name = "__codelineno-12-2" href = "#__codelineno-12-2" > < / a > < span class = "w" > < / span > < span class = "sd" > " " " 二分查找最右一个元素" " " < / span >
< a id = "__codelineno-12-3" name = "__codelineno-12-3" href = "#__codelineno-12-3" > < / a > < span class = "c1" > # 初始化双闭区间 [0, n-1] ,即 i, j 分别指向数组首元素、尾元素< / span >
< a id = "__codelineno-12-4" name = "__codelineno-12-4" href = "#__codelineno-12-4" > < / a > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-12-5" name = "__codelineno-12-5" href = "#__codelineno-12-5" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-6" name = "__codelineno-12-6" href = "#__codelineno-12-6" > < / a > < span class = "n" > m< / span > < span class = "o" > =< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "n" > j< / span > < span class = "p" > )< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "c1" > # 计算中点索引 m< / span >
< a id = "__codelineno-12-7" name = "__codelineno-12-7" href = "#__codelineno-12-7" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-8" name = "__codelineno-12-8" href = "#__codelineno-12-8" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-12-9" name = "__codelineno-12-9" href = "#__codelineno-12-9" > < / a > < span class = "k" > elif< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-10" name = "__codelineno-12-10" href = "#__codelineno-12-10" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-12-11" name = "__codelineno-12-11" href = "#__codelineno-12-11" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-12" name = "__codelineno-12-12" href = "#__codelineno-12-12" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 首个大于 target 的元素在区间 [m+1, j] 中< / span >
< a id = "__codelineno-12-13" name = "__codelineno-12-13" href = "#__codelineno-12-13" > < / a > < span class = "k" > if< / span > < span class = "n" > j< / span > < span class = "o" > ==< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "ow" > or< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > j< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-14" name = "__codelineno-12-14" href = "#__codelineno-12-14" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-12-15" name = "__codelineno-12-15" href = "#__codelineno-12-15" > < / a > < span class = "k" > return< / span > < span class = "n" > j< / span >
< a id = "__codelineno-12-3" name = "__codelineno-12-3" href = "#__codelineno-12-3" > < / a > < span class = "n" > i< / span > < span class = "p" > ,< / span > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-12-4" name = "__codelineno-12-4" href = "#__codelineno-12-4" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-5" name = "__codelineno-12-5" href = "#__codelineno-12-5" > < / a > < span class = "n" > m< / span > < span class = "o" > =< / span > < span class = "p" > (< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "n" > j< / span > < span class = "p" > )< / span > < span class = "o" > //< / span > < span class = "mi" > 2< / span > < span class = "c1" > # 计算中点索引 m< / span >
< a id = "__codelineno-12-6" name = "__codelineno-12-6" href = "#__codelineno-12-6" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-7" name = "__codelineno-12-7" href = "#__codelineno-12-7" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-12-8" name = "__codelineno-12-8" href = "#__codelineno-12-8" > < / a > < span class = "k" > elif< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-9" name = "__codelineno-12-9" href = "#__codelineno-12-9" > < / a > < span class = "n" > j< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-12-10" name = "__codelineno-12-10" href = "#__codelineno-12-10" > < / a > < span class = "k" > else< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-11" name = "__codelineno-12-11" href = "#__codelineno-12-11" > < / a > < span class = "n" > i< / span > < span class = "o" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 首个大于 target 的元素在区间 [m+1, j] 中< / span >
< a id = "__codelineno-12-12" name = "__codelineno-12-12" href = "#__codelineno-12-12" > < / a > < span class = "k" > if< / span > < span class = "n" > j< / span > < span class = "o" > ==< / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > )< / span > < span class = "ow" > or< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > j< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > :< / span >
< a id = "__codelineno-12-13" name = "__codelineno-12-13" href = "#__codelineno-12-13" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > # 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-12-14" name = "__codelineno-12-14" href = "#__codelineno-12-14" > < / a > < span class = "k" > return< / span > < span class = "n" > j< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-13-1" name = "__codelineno-13-1" href = "#__codelineno-13-1" > < / a > < span class = "p" > [< / span > < span class = "nx" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "kd" > func< / span > < span class = "p" > ]{< / span > < span class = "nx" > binarySearchRightEdge< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > binary_search_edge.go< / span > < pre > < span > < / span > < code > < a id = "__codelineno-13-1" name = "__codelineno-13-1" href = "#__codelineno-13-1" > < / a > < span class = "cm" > /* 二分查找最右一个元素 */< / span >
< a id = "__codelineno-13-2" name = "__codelineno-13-2" href = "#__codelineno-13-2" > < / a > < span class = "kd" > func< / span > < span class = "w" > < / span > < span class = "nx" > binarySearchRightEdge< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "w" > < / span > < span class = "p" > []< / span > < span class = "kt" > int< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "p" > )< / span > < span class = "w" > < / span > < span class = "kt" > int< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-3" name = "__codelineno-13-3" href = "#__codelineno-13-3" > < / a > < span class = "w" > < / span > < span class = "c1" > // 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-13-4" name = "__codelineno-13-4" href = "#__codelineno-13-4" > < / a > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "p" > ,< / span > < span class = "w" > < / span > < span class = "nb" > len< / span > < span class = "p" > (< / span > < span class = "nx" > nums< / span > < span class = "p" > )< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-13-5" name = "__codelineno-13-5" href = "#__codelineno-13-5" > < / a > < span class = "w" > < / span > < span class = "k" > for< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > < =< / span > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-6" name = "__codelineno-13-6" href = "#__codelineno-13-6" > < / a > < span class = "w" > < / span > < span class = "c1" > // 计算中点索引 m< / span >
< a id = "__codelineno-13-7" name = "__codelineno-13-7" href = "#__codelineno-13-7" > < / a > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > :=< / span > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "p" > (< / span > < span class = "nx" > j< / span > < span class = "o" > -< / span > < span class = "nx" > i< / span > < span class = "p" > )< / span > < span class = "o" > /< / span > < span class = "mi" > 2< / span >
< a id = "__codelineno-13-8" name = "__codelineno-13-8" href = "#__codelineno-13-8" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > m< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-9" name = "__codelineno-13-9" href = "#__codelineno-13-9" > < / a > < span class = "w" > < / span > < span class = "c1" > // target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-13-10" name = "__codelineno-13-10" href = "#__codelineno-13-10" > < / a > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-13-11" name = "__codelineno-13-11" href = "#__codelineno-13-11" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > m< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "p" > > < / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-12" name = "__codelineno-13-12" href = "#__codelineno-13-12" > < / a > < span class = "w" > < / span > < span class = "c1" > // target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-13-13" name = "__codelineno-13-13" href = "#__codelineno-13-13" > < / a > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-13-14" name = "__codelineno-13-14" href = "#__codelineno-13-14" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span > < span class = "w" > < / span > < span class = "k" > else< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-15" name = "__codelineno-13-15" href = "#__codelineno-13-15" > < / a > < span class = "w" > < / span > < span class = "c1" > // 首个大于 target 的元素在区间 [m+1, j] 中< / span >
< a id = "__codelineno-13-16" name = "__codelineno-13-16" href = "#__codelineno-13-16" > < / a > < span class = "w" > < / span > < span class = "nx" > i< / span > < span class = "w" > < / span > < span class = "p" > =< / span > < span class = "w" > < / span > < span class = "nx" > m< / span > < span class = "w" > < / span > < span class = "o" > +< / span > < span class = "w" > < / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-13-17" name = "__codelineno-13-17" href = "#__codelineno-13-17" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-13-18" name = "__codelineno-13-18" href = "#__codelineno-13-18" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-13-19" name = "__codelineno-13-19" href = "#__codelineno-13-19" > < / a > < span class = "w" > < / span > < span class = "k" > if< / span > < span class = "w" > < / span > < span class = "nx" > j< / span > < span class = "w" > < / span > < span class = "p" > < < / span > < span class = "w" > < / span > < span class = "mi" > 0< / span > < span class = "w" > < / span > < span class = "o" > ||< / span > < span class = "w" > < / span > < span class = "nx" > nums< / span > < span class = "p" > [< / span > < span class = "nx" > j< / span > < span class = "p" > ]< / span > < span class = "w" > < / span > < span class = "o" > !=< / span > < span class = "w" > < / span > < span class = "nx" > target< / span > < span class = "w" > < / span > < span class = "p" > {< / span >
< a id = "__codelineno-13-20" name = "__codelineno-13-20" href = "#__codelineno-13-20" > < / a > < span class = "w" > < / span > < span class = "c1" > // 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-13-21" name = "__codelineno-13-21" href = "#__codelineno-13-21" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-13-22" name = "__codelineno-13-22" href = "#__codelineno-13-22" > < / a > < span class = "w" > < / span > < span class = "p" > }< / span >
< a id = "__codelineno-13-23" name = "__codelineno-13-23" href = "#__codelineno-13-23" > < / a > < span class = "w" > < / span > < span class = "k" > return< / span > < span class = "w" > < / span > < span class = "nx" > j< / span >
< a id = "__codelineno-13-24" name = "__codelineno-13-24" href = "#__codelineno-13-24" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
@ -2142,7 +2205,26 @@
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >
< div class = "highlight" > < span class = "filename" > binary_search_edge.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-18-1" name = "__codelineno-18-1" href = "#__codelineno-18-1" > < / a > < span class = "p" > [< / span > < span class = "kd" > class< / span > < span class = "p" > ]{}< / span > < span class = "o" > -< / span > < span class = "p" > [< / span > < span class = "kd" > func< / span > < span class = "p" > ]{< / span > < span class = "n" > binarySearchRightEdge< / span > < span class = "p" > }< / span >
< div class = "highlight" > < span class = "filename" > binary_search_edge.swift< / span > < pre > < span > < / span > < code > < a id = "__codelineno-18-1" name = "__codelineno-18-1" href = "#__codelineno-18-1" > < / a > < span class = "cm" > /* 二分查找最右一个元素 */< / span >
< a id = "__codelineno-18-2" name = "__codelineno-18-2" href = "#__codelineno-18-2" > < / a > < span class = "kd" > func< / span > < span class = "nf" > binarySearchRightEdge< / span > < span class = "p" > (< / span > < span class = "n" > nums< / span > < span class = "p" > :< / span > < span class = "p" > [< / span > < span class = "nb" > Int< / span > < span class = "p" > ],< / span > < span class = "n" > target< / span > < span class = "p" > :< / span > < span class = "nb" > Int< / span > < span class = "p" > )< / span > < span class = "p" > -> < / span > < span class = "nb" > Int< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-3" name = "__codelineno-18-3" href = "#__codelineno-18-3" > < / a > < span class = "c1" > // 初始化双闭区间 [0, n-1]< / span >
< a id = "__codelineno-18-4" name = "__codelineno-18-4" href = "#__codelineno-18-4" > < / a > < span class = "kd" > var< / span > < span class = "nv" > i< / span > < span class = "p" > =< / span > < span class = "mi" > 0< / span >
< a id = "__codelineno-18-5" name = "__codelineno-18-5" href = "#__codelineno-18-5" > < / a > < span class = "kd" > var< / span > < span class = "nv" > j< / span > < span class = "p" > =< / span > < span class = "n" > nums< / span > < span class = "p" > .< / span > < span class = "bp" > count< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span >
< a id = "__codelineno-18-6" name = "__codelineno-18-6" href = "#__codelineno-18-6" > < / a > < span class = "k" > while< / span > < span class = "n" > i< / span > < span class = "o" > < =< / span > < span class = "n" > j< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-7" name = "__codelineno-18-7" href = "#__codelineno-18-7" > < / a > < span class = "kd" > let< / span > < span class = "nv" > m< / span > < span class = "p" > =< / span > < span class = "n" > i< / span > < span class = "o" > +< / span > < span class = "p" > (< / span > < span class = "n" > j< / span > < span class = "o" > -< / span > < span class = "n" > i< / span > < span class = "p" > )< / span > < span class = "o" > /< / span > < span class = "mi" > 2< / span > < span class = "c1" > // 计算中点索引 m< / span >
< a id = "__codelineno-18-8" name = "__codelineno-18-8" href = "#__codelineno-18-8" > < / a > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > < < / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-9" name = "__codelineno-18-9" href = "#__codelineno-18-9" > < / a > < span class = "n" > i< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > // target 在区间 [m+1, j] 中< / span >
< a id = "__codelineno-18-10" name = "__codelineno-18-10" href = "#__codelineno-18-10" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "k" > if< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > m< / span > < span class = "p" > ]< / span > < span class = "o" > > < / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-11" name = "__codelineno-18-11" href = "#__codelineno-18-11" > < / a > < span class = "n" > j< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > // target 在区间 [i, m-1] 中< / span >
< a id = "__codelineno-18-12" name = "__codelineno-18-12" href = "#__codelineno-18-12" > < / a > < span class = "p" > }< / span > < span class = "k" > else< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-13" name = "__codelineno-18-13" href = "#__codelineno-18-13" > < / a > < span class = "n" > i< / span > < span class = "p" > =< / span > < span class = "n" > m< / span > < span class = "o" > +< / span > < span class = "mi" > 1< / span > < span class = "c1" > // 首个大于 target 的元素在区间 [m+1, j] 中< / span >
< a id = "__codelineno-18-14" name = "__codelineno-18-14" href = "#__codelineno-18-14" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-18-15" name = "__codelineno-18-15" href = "#__codelineno-18-15" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-18-16" name = "__codelineno-18-16" href = "#__codelineno-18-16" > < / a > < span class = "k" > if< / span > < span class = "n" > j< / span > < span class = "o" > < < / span > < span class = "mi" > 0< / span > < span class = "o" > ||< / span > < span class = "n" > nums< / span > < span class = "p" > [< / span > < span class = "n" > j< / span > < span class = "p" > ]< / span > < span class = "o" > !=< / span > < span class = "n" > target< / span > < span class = "p" > {< / span >
< a id = "__codelineno-18-17" name = "__codelineno-18-17" href = "#__codelineno-18-17" > < / a > < span class = "k" > return< / span > < span class = "o" > -< / span > < span class = "mi" > 1< / span > < span class = "c1" > // 未找到目标元素,返回 -1< / span >
< a id = "__codelineno-18-18" name = "__codelineno-18-18" href = "#__codelineno-18-18" > < / a > < span class = "p" > }< / span >
< a id = "__codelineno-18-19" name = "__codelineno-18-19" href = "#__codelineno-18-19" > < / a > < span class = "k" > return< / span > < span class = "n" > j< / span >
< a id = "__codelineno-18-20" name = "__codelineno-18-20" href = "#__codelineno-18-20" > < / a > < span class = "p" > }< / span >
< / code > < / pre > < / div >
< / div >
< div class = "tabbed-block" >