浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶

Posted 鍓嶇椋熷爞

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶相关的知识,希望对你有一定的参考价值。

杩欐槸鍓嶇椋熷爞鐨勭34绡囧師鍒?/strong>  

锛堢粰鍓嶇椋熷爞鍔犳槦鏍囷紝鍚冨ソ姣忎竴椤匡級

銆岃鎰熷害锛氿煂燄煂燄煂燄煂燄煂熴€?/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>



  鍦ㄧ湅鍜岃浆鍙戞槸鑾ぇ榧撳姳鉂わ笍

以上是关于浠庨厭妗屾父鎴忕湅浜屽垎鏌ユ壘绠楁硶的主要内容,如果未能解决你的问题,请参考以下文章

Golang-鏌ユ壘(浜屽垎娉曟煡鎵?

绠楁硶6---鏌ユ壘

浜屽弶鏌ユ壘鏍?Java瀹炵幇