浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶
Posted 鍓嶇椋熷爞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶相关的知识,希望对你有一定的参考价值。
锛堢粰鍓嶇椋熷爞鍔犳槦鏍囷紝鍚冨ソ姣忎竴椤匡級
銆岃鎰熷害锛氿煂燄煂燄煂燄煂燄煂熴€?/strong>
銆屽彛鍛筹細瀛滅劧鐗涜倝銆?/strong>
銆岀児楗椂闂达細5min銆?/strong>
鏈枃宸叉敹褰曞湪
Github
github.com/Geekhyt锛屾劅璋tar銆?/p>
閰掓涓婃浘缁忕帺杩囪繖鏍蜂竴涓皬娓告垙锛屾父鎴忚鍒欐槸锛氫富鎸佷汉姣忔闅忔満浠?1-1000 涓€夋嫨涓€涓暟瀛楋紝姣斿鏄?171銆傚彧鏈変富鎸佷汉鑷繁鐭ラ亾骞朵簨鍏堝啓鍦ㄧ焊鏉′笂鐣欏瓨锛岀劧鍚庡垎鍒澶у鏉ョ寽锛岃兘澶熺敤鏈€灏戞鏁扮寽鍒扮殑浜鸿幏鑳滃苟鎷ユ湁鎸囧畾涓€涓汉缃氶厭鐨勬潈鍒┿€?/p>
-
绔ユ宸达細500 -
涓绘寔浜猴細澶т簡 -
绔ユ宸达細250 -
涓绘寔浜猴細澶т簡 -
绔ユ宸达細125 -
涓绘寔浜猴細灏忎簡 -
绔ユ宸达細187 -
涓绘寔浜猴細澶т簡 -
绔ユ宸达細156 -
涓绘寔浜猴細灏忎簡 -
绔ユ宸达細171
涓绘寔浜哄啀娆℃寫閫夋暟瀛楋紝璁╂墥钂滃皬濡瑰幓鐚?..
鏈€鍚庯紝绔ユ宸寸敤鐨勬鏁版渶灏戯紝绔ユ宸磋幏鑳滐紒鎸囧畾鎵掕挏灏忓缃氶厭銆?/p>
杩欎釜娓告垙灏辨槸鐪嬭皝鑳戒娇鐢ㄦ渶灏戠殑娆℃暟鐚滃埌涓绘寔浜洪€夌殑鏁板瓧锛岃皝灏辫幏鑳溿€傝繖绉嶅湪鏈夊簭鏁版嵁闆嗗悎涓殑鏌ユ壘鐢ㄤ簩鍒嗘煡鎵惧啀鍚堥€備笉杩囦簡銆?/p>
浜屽垎鏌ユ壘 Binary Search
浜屽垎鏌ユ壘锛岄【鍚嶆€濅箟銆?鐪嬩笂鏂囨宸寸啛缁冪殑鐏岄厭鎿嶄綔涔熷彲浠ョ煡閬?姣忔鐨勬煡鎵鹃兘鏄拰鍖洪棿鐨勪腑闂村厓绱犲姣旓紝灏嗗緟鏌ユ壘鐨勫尯闂寸缉灏忎负涓€鍗婏紝鐩村埌鎵惧埌鐩爣鍏冪礌锛屾垨鑰呭尯闂磋缂╁皬涓?0 (娌℃壘鍒?銆備簩鍒嗘煡鎵剧殑鏃堕棿澶嶆潅搴︽槸 O(logn)
銆傚姣斿父閲忕骇鏃堕棿澶嶆潅搴︼紝褰撳父閲忓緢澶ф椂 O(999999)
锛屽氨浼氭瘮 O(1)
鐨勭畻娉曡楂樻晥銆?/p>
浜屽垎绠楁硶铏界劧楂樻晥锛屼絾涔熷瓨鍦ㄤ竴瀹氱殑灞€闄愭€с€傛兂瑕佷娇浜屽垎鏌ユ壘鍙戞尌濞佸姏锛岄渶瑕佹弧瓒冲嚑涓墠缃潯浠舵墠琛屻€?/p>
-
鏈夊簭(鍗曡皟閫掑/閫掑噺) -
鏁扮粍(鑳藉閫氳繃绱㈠紩璁块棶) -
鏁版嵁閲忎笉鑳藉お澶?鏁扮粍鍐呭瓨绌洪棿杩炵画锛屽鍐呭瓨瑕佹眰涓ユ牸)涔熶笉鑳藉お灏?閬嶅巻鍗冲彲)
LeetCode 鐪熼
33. 鎼滅储鏃嬭浆鎺掑簭鏁扮粍
https://leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/pythonjs-er-fen-fa-33-sou-suo-xuan-zhuan-pai-xu-sh/
鍋囪鎸夌収鍗囧簭鎺掑簭鐨勬暟缁勫湪棰勫厛鏈煡鐨勬煇涓偣涓婅繘琛屼簡鏃嬭浆銆?/p>
( 渚嬪锛屾暟缁?nbsp;[0,1,2,4,5,6,7] 鍙兘鍙樹负 [4,5,6,7,0,1,2] )銆?/p>
鎼滅储涓€涓粰瀹氱殑鐩爣鍊硷紝濡傛灉鏁扮粍涓瓨鍦ㄨ繖涓洰鏍囧€硷紝鍒欒繑鍥炲畠鐨勭储寮曪紝鍚﹀垯杩斿洖 -1 銆?/p>
浣犲彲浠ュ亣璁炬暟缁勪腑涓嶅瓨鍦ㄩ噸澶嶇殑鍏冪礌銆?/p>
浣犵殑绠楁硶鏃堕棿澶嶆潅搴﹀繀椤绘槸 O(log n) 绾у埆銆?/p>
绀轰緥 1:
杈撳叆: nums = [4,5,6,7,0,1,2], target = 0 杈撳嚭: 4
绀轰緥 2:
杈撳叆: nums = [4,5,6,7,0,1,2], target = 3 杈撳嚭: -1
鍏抽敭鐐?/span>
杩涜鏃嬭浆鍚庣殑鏁扮粍涓€瀹氭湁涓€閮ㄥ垎鏄湁搴忕殑
銆傝€屼笖锛岄鐩姹傛椂闂村鏉傚害涓?O(logn)
锛屾殫绀烘垜浠娇鐢ㄤ簩鍒嗘悳绱€?/p>
濡備笂鍥句腑鐨勪袱绉嶆儏鍐碉紝瑙傚療鏃嬭浆鍚庣殑鏁扮粍锛?/p>
-
nums[mid] >= nums[start] 鏃讹紝mid 鍦ㄥ乏杈逛笖宸﹁竟鏈夊簭 5 >= 2
-
nums[mid] < nums[start] 鏃讹紝mid 鍦ㄥ彸杈逛笖鍙宠竟鏈夊簭 2 < 6
鎺ョ潃鎴戜滑鏉ュ垽鏂?target
鍦ㄥ摢涓€涓儴鍒嗭紝鑸嶅純鍙︿竴閮ㄥ垎鍗冲彲銆傚涓婂浘鐨勭浜岀鎯呭喌锛屾垜浠亣璁?target
鏄?榛戣壊鐨?3
銆?code class="mq-89">mid 鍦ㄥ彸杈逛篃灏辨槸 [mid, end]
锛?code class="mq-91">target > nums[mid] && target <= nums[end]锛屾墍浠ヨ垗寮冨乏杈癸紝start = mid + 1
銆?/p>
const search = function(nums, target) {
let start = 0;
let end = nums.length - 1;
while (start <= end) {
const mid = start + ((end - start) >> 1);
if (nums[mid] === target) {
return mid;
}
// 宸︿晶鏈夊簭
if (nums[mid] >= nums[start]) {
// target 鍦?nbsp;[start, mid] 涔嬮棿
if (target >= nums[start] && target < nums[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
} else { // 鍙充晶鏈夊簭
// target 鍦?nbsp;[mid, end] 涔嬮棿
if (target > nums[mid] && target <= nums[end]) {
start = mid + 1;
} else {
end = mid - 1;
}
}
}
return -1;
}
澶嶆潅搴﹀垎鏋?/span>
-
鏃堕棿澶嶆潅搴︼細 O(logn) -
绌洪棿澶嶆潅搴︼細 O(1) 鍙渶瑕佸父閲忕骇鍒殑绌洪棿瀛樻斁鍙橀噺
鉂わ笍鐖卞績涓夎繛鍑?/span>
1.鐪嬪埌杩欓噷浜嗗氨鐐逛釜鍦ㄧ湅鏀寔涓嬪惂锛屼綘鐨?span class="mq-135">銆屽湪鐪嬨€?/strong>鏄垜鍒涗綔鐨勫姩鍔涖€?/p>
3.鏈枃宸叉敹褰曞湪鍓嶇椋熷爞Github
github.com/Geekhyt锛屾眰涓皬鏄熸槦锛屾劅璋tar銆?/p>
鎺橀噾锛氱娆у反
鐭ヤ箮锛氱娆у反
杩欐槸涓€涓粓韬涔犵殑鐢蜂汉锛屼粬鍦ㄥ潥鎸佽嚜宸辩儹鐖辩殑浜嬫儏锛屾杩庡姞鍏ュ墠绔鍫傦紝鍜岃繖涓敺浜轰竴璧峰紑蹇冪殑鍙樿儢锝?/span>
鎺ㄨ崘闃呰锛?/span>
鍦ㄧ湅鍜岃浆鍙戞槸鑾ぇ榧撳姳鉂わ笍
以上是关于浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶的主要内容,如果未能解决你的问题,请参考以下文章