是if a[start] <= target < a[mid]一樣的a[start] <= target and target<a[mid] and a[start] < a[mid]嗎?(我認為不是,但在視覺上看起來兩者是相同的)這在引擎蓋下是如何工作的?在 SO 上搜索,但找不到答案。
1 回答

絕地無雙
TA貢獻1946條經驗 獲得超4個贊
這
if a[start] <= target < a[mid]:
本質上 [*] 與
if a[start] <= target and target < a[mid]:
(如果為真,那么a[start] < a[mid]
, since<=
應該是可傳遞的。)
[*] 有一個微妙之處不適用于您的案例,但值得了解。鏈式形式只計算一次中間表達式,而擴展形式計算兩次。如果中間表達式的計算成本很高或有一些副作用(例如在屏幕上打印某些內容),這可能很重要。
相關文檔:https ://docs.python.org/3/reference/expressions.html#comparisons
添加回答
舉報
0/150
提交
取消