銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫?/a>

Posted 鍓嶇妫灄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫?/a>相关的知识,希望对你有一定的参考价值。

寮曡█锛堟枃鏈湁绂忓埄锛夝煆?/h2>

绠楁硶涓€鐩存槸澶у巶鍓嶇闈㈣瘯甯搁棶鐨勪竴鍧楋紝鑰屽ぇ瀹跺線寰€鍑嗗杩欐柟闈㈢殑闈㈣瘯閮芥槸閫氳繃leetcode鍒烽銆?/p>

鎴戠壒鍦版暣鐞嗕簡鍑犻亾leetcode涓€?code>寰堟湁鎰忔€?/code>銆嶈€屼笖闈炲父銆?code>楂橀銆嶇殑绠楁硶棰樼洰锛屽垎鍒粰鍑轰簡鎬濊矾鍒嗘瀽锛堝甫鍥捐В锛夊拰浠g爜瀹炵幇銆?/p>

璁ょ湡浠旂粏鐨勯槄璇诲畬鏈枃锛岀浉淇″浜庝綘鍦ㄧ畻娉曟柟闈㈢殑闈㈣瘯涓€瀹氫細鏈変笉灏忕殑甯姪锛侌煠?/p>

涓ゆ暟涔嬪拰 馃

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸搱甯岃〃

棰樼洰鎻忚堪

缁欏畾涓€涓暣鏁版暟缁?nums聽 鍜屼竴涓洰鏍囧€?target锛岃浣犲湪璇ユ暟缁勪腑鎵惧嚭鍜屼负鐩爣鍊肩殑閭?code>涓や釜鏁存暟锛屽苟杩斿洖浠栦滑鐨勬暟缁勪笅鏍囥€?/p>

浣犲彲浠ュ亣璁炬瘡绉嶈緭鍏ュ彧浼氬搴斾竴涓瓟妗堛€備絾鏄紝鏁扮粍涓悓涓€涓厓绱犱笉鑳戒娇鐢ㄤ袱閬嶃€?/p>

绀轰緥锛?/p>

缁欏畾 nums = [2, 7, 11, 15], target = 9

鍥犱负 nums[0] + nums[1] = 2 + 7 = 9
鎵€浠ヨ繑鍥?[0, 1]

鎬濊矾鍒嗘瀽

澶у鏁板悓瀛︾湅鍒拌繖閬撻鐩紝蹇冧腑鑲畾浼氭兂锛氳繖閬撻鐩お绠€鍗曚簡锛屼笉灏变袱灞傞亶鍘嗗槢锛氫袱灞傚惊鐜潵閬嶅巻鍚屼竴涓暟缁勶紱绗竴灞傚惊鐜亶鍘嗙殑鍊艰涓?code>a锛岀浜屽眰寰幆鏃堕亶鍘嗙殑鍊艰涓?code>b锛涜嫢a+b = 鐩爣鍊?/code>锛岄偅涔?code>a鍜?code>b瀵瑰簲鐨勬暟缁勪笅鏍囧氨鏄垜浠兂瑕佺殑绛旀銆?/p>

杩欑瑙f硶娌℃瘺鐥咃紝浣嗘湁娌℃湁浼樺寲鐨勬柟妗堝憿锛燄煠?/p>

瑕佺煡閬撲袱灞傚惊鐜緢澶氭儏鍐典笅閮芥剰鍛崇潃O(n^2) 鐨勫鏉傚害锛岃繖涓鏉傚害闈炲父瀹规槗瀵艰嚧浣犵殑绠楁硶瓒呮椂銆傚嵆渚挎病鏈夎秴鏃讹紝鍦ㄦ槑鏄庢湁涓€灞傞亶鍘嗚В娉曠殑鎯呭喌涓嬶紝浣犲啓浜嗕袱灞傞亶鍘嗭紝闈㈣瘯瀹樹篃浼氬浣犵殑鍗拌薄鍒嗗ぇ鎵撴姌鎵c€傪煠?/p>

鍏跺疄鎴戜滑鍙互鍦ㄩ亶鍘嗘暟缁勭殑杩囩▼涓紝澧炲姞涓€涓?code>Map缁撴瀯鏉ュ瓨鍌ㄥ凡缁忛亶鍘嗚繃鐨勬暟瀛楀強鍏跺搴旂殑绱㈠紩鍊笺€傜劧鍚庢瘡閬嶅巻鍒颁竴涓柊鏁板瓧鐨勬椂鍊欙紝閮藉洖鍒?code>Map閲屽幓鏌ヨtargetNum涓庤鏁扮殑宸€兼槸鍚﹀凡缁忓湪鍓嶉潰鐨勬暟瀛椾腑鍑虹幇杩囦簡銆傝嫢鍑虹幇杩囷紝閭d箞绛旀宸茬劧鏄剧幇锛屾垜浠氨涓嶅繀鍐嶅線涓嬭蛋浜嗐€?/p>

鎴戜滑灏变互鏈涓殑渚嬪瓙缁撳悎鍥剧墖鏉ヨ鏄庝竴涓嬩笂闈㈡彁鍒扮殑杩欑鎬濊矾锛?/p>

  • 杩欓噷鐢ㄥ璞?code>diffs鏉ユā鎷?code>map缁撴瀯锛?br>

    棣栧厛閬嶅巻鏁扮粍绗竴涓厓绱狅紝姝ゆ椂key涓?2锛?code>value涓虹储寮?0

  • 寰€涓嬮亶鍘嗭紝閬囧埌浜?7:

    璁$畻targetNum鍜?7 鐨勫樊鍊间负 2锛屽幓diffs涓绱?2 杩欎釜key锛屽彂鐜版槸涔嬪墠鍑虹幇杩囩殑鍊笺€傞偅涔堟湰棰樼殑绛旀灏卞嚭鏉ヤ簡锛?/p>

浠g爜瀹炵幇

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */
const twoSum = function (nums, target) {
  const diffs = {};
  // 缂撳瓨鏁扮粍闀垮害
  const len = nums.length;
  // 閬嶅巻鏁扮粍
  for (let i = 0; i < len; i++) {
    // 鍒ゆ柇褰撳墠鍊煎搴旂殑 target 宸€兼槸鍚﹀瓨鍦?    if (diffs[target - nums[i]] !== undefined) {
      // 鑻ユ湁瀵瑰簲宸€硷紝閭d箞寰楀埌绛旀
      return [diffs[target - nums[i]], i];
    }
    // 鑻ユ病鏈夊搴斿樊鍊硷紝鍒欒褰曞綋鍓嶅€?    diffs[nums[i]] = i;
  }
};

涓夋暟涔嬪拰 馃

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸弻鎸囬拡

棰樼洰鎻忚堪

缁欎綘涓€涓寘鍚?code>n涓暣鏁扮殑鏁扮粍nums锛屽垽鏂?code>nums涓槸鍚﹀瓨鍦ㄤ笁涓厓绱?code>a锛?code>b锛?code>c 锛屼娇寰?code>a + b + c = 0銆傝浣犳壘鍑烘墍鏈夋弧瓒虫潯浠朵笖涓嶉噸澶嶇殑涓夊厓缁勩€?/p>

娉ㄦ剰锛氱瓟妗堜腑涓嶅彲浠ュ寘鍚噸澶嶇殑涓夊厓缁勩€?/p>

绀轰緥锛?/p>

缁欏畾鏁扮粍 nums = [-1, 0, 1, 2, -1, -4]锛?
婊¤冻瑕佹眰鐨勪笁鍏冪粍闆嗗悎涓猴細
[
  [-1, 0, 1],
  [-1, -1, 2]
]

鎬濊矾鍒嗘瀽

鍜屼笂闈㈢殑涓ゆ暟涔嬪拰涓€鏍凤紝濡傛灉涓嶈鐪熸€濊€冿紝鏈€蹇殑鏂瑰紡鍙兘灏辨槸澶氬眰閬嶅巻浜嗐€備絾鏈変簡鍓嶈溅涔嬮壌锛屾垜浠悓鏍峰彲浠ユ妸姹傚拰闂鍙樹负姹傚樊闂锛氬浐瀹氬叾涓竴涓暟锛屽湪鍓╀笅鐨勬暟涓鎵炬槸鍚︽湁涓や釜鏁扮殑鍜岃繖涓浐瀹氭暟鐩稿姞鏄瓑浜?0 鐨勩€?/p>

杩欓噷鎴戜滑閲囩敤鍙屾寚閽堟硶鏉ヨВ鍐抽棶棰橈紝鐩告瘮涓夊眰寰幆锛屾晥鐜囦細澶уぇ鎻愬崌銆?/p>

鍙屾寚閽堟硶鐨勯€傜敤鑼冨洿姣旇緝骞匡紝涓€鑸儚姹傚拰銆佹瘮澶у皬鐨勯兘鍙互鐢ㄥ畠鏉ヨВ鍐炽€備絾鏄湁涓€涓墠鎻愶細鏁扮粍蹇呴』鏈夊簭

鍥犳鎴戜滑鐨勭涓€姝ュ氨鏄厛灏嗘暟缁勮繘琛屾帓搴忥細

// 缁?nums 鎺掑簭
nums = nums.sort((a, b) => {
  return a - b;
});

鐒跺悗瀵规暟缁勮繘琛岄亶鍘嗭紝姣忛亶鍘嗗埌鍝釜鏁板瓧锛屽氨鍥哄畾褰撳墠鐨勬暟瀛椼€傚悓鏃跺乏鎸囬拡鎸囧悜璇ユ暟瀛楀悗闈㈢殑绱ч偦鐨勯偅涓暟瀛楋紝鍙虫寚閽堟寚鍚戞暟缁勬湯灏俱€傜劧鍚庡乏鍙虫寚閽堝垎鍒悜涓棿闈犳嫝锛?br>

姣忔鎸囬拡绉诲姩涓€娆′綅缃紝灏辫绠椾竴涓嬩袱涓寚閽堟寚鍚戞暟瀛椾箣鍜屽姞涓婂浐瀹氱殑閭d釜鏁颁箣鍚庯紝鏄惁绛変簬 0銆傚鏋滄槸锛岄偅涔堟垜浠氨寰楀埌浜嗕竴涓洰鏍囩粍鍚堬紱鍚﹀垯锛屽垎涓ょ鎯呭喌鏉ョ湅锛?/p>

  • 鐩稿姞涔嬪拰澶т簬 0锛岃鏄庡彸渚х殑鏁板亸澶т簡锛屽彸鎸囬拡宸︾Щ
  • 鐩稿姞涔嬪拰灏忎簬 0锛岃鏄庡乏渚х殑鏁板亸灏忎簡锛屽乏鎸囬拡鍙崇Щ

浠g爜瀹炵幇

/**
 * @param {number[]} nums
 * @return {number[][]}
 */
const threeSum = function (nums) {
  // 鐢ㄤ簬瀛樻斁缁撴灉鏁扮粍
  let res = [];
  // 鐩爣鍊间负0
  let sum = 0;
  // 缁?nums 鎺掑簭
  nums = nums.sort((a, b) => {
    return a - b;
  });
  // 缂撳瓨鏁扮粍闀垮害
  const len = nums.length;
  for (let i = 0; i < len - 2; i++) {
    // 宸︽寚閽?j
    let j = i + 1;
    // 鍙虫寚閽坘
    let k = len - 1;
    // 濡傛灉閬囧埌閲嶅鐨勬暟瀛楋紝鍒欒烦杩?    if (i > 0 && nums[i] === nums[i - 1]) {
      continue;
    }
    while (j < k) {
      // 涓夋暟涔嬪拰灏忎簬0锛屽乏鎸囬拡鍓嶈繘
      if (nums[i] + nums[j] + nums[k] < 0) {
        j++;
        // 澶勭悊宸︽寚閽堝厓绱犻噸澶嶇殑鎯呭喌
        while (j < k && nums[j] === nums[j - 1]) {
          j++;
        }
      } else if (nums[i] + nums[j] + nums[k] > 0) {
        // 涓夋暟涔嬪拰澶т簬0锛屽彸鎸囬拡鍚庨€€
        k--;

        // 澶勭悊鍙虫寚閽堝厓绱犻噸澶嶇殑鎯呭喌
        while (j < k && nums[k] === nums[k + 1]) {
          k--;
        }
      } else {
        // 寰楀埌鐩爣鏁板瓧缁勫悎锛屾帹鍏ョ粨鏋滄暟缁?        res.push([nums[i], nums[j], nums[k]]);

        // 宸﹀彸鎸囬拡涓€璧峰墠杩?        j++;
        k--;

        // 鑻ュ乏鎸囬拡鍏冪礌閲嶅锛岃烦杩?        while (j < k && nums[j] === nums[j - 1]) {
          j++;
        }

        // 鑻ュ彸鎸囬拡鍏冪礌閲嶅锛岃烦杩?        while (j < k && nums[k] === nums[k + 1]) {
          k--;
        }
      }
    }
  }

  // 杩斿洖缁撴灉鏁扮粍
  return res;
};

鐩涙渶澶氭按鐨勫鍣?馃

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸弻鎸囬拡

棰樼洰鎻忚堪

缁欎綘 n 涓潪璐熸暣鏁?a1锛宎2锛?..锛宎n锛屾瘡涓暟浠h〃鍧愭爣涓殑涓€涓偣 聽(i,聽ai) 銆傚湪鍧愭爣鍐呯敾 n 鏉″瀭鐩寸嚎锛屽瀭鐩寸嚎 i聽 鐨勪袱涓鐐瑰垎鍒负 聽(i,聽ai) 鍜?(i, 0)銆傛壘鍑哄叾涓殑涓ゆ潯绾匡紝浣垮緱瀹冧滑涓?聽x聽 杞村叡鍚屾瀯鎴愮殑瀹瑰櫒鍙互瀹圭撼鏈€澶氱殑姘淬€?/p>

璇存槑锛氫綘涓嶈兘鍊炬枩瀹瑰櫒锛屼笖 聽n聽 鐨勫€艰嚦灏戜负 2銆?br>

鍥句腑鍨傜洿绾夸唬琛ㄨ緭鍏ユ暟缁刐1,8,6,2,5,4,8,3,7]銆傚湪姝ゆ儏鍐典笅锛屽鍣ㄨ兘澶熷绾虫按锛堣〃绀轰负钃濊壊閮ㄥ垎锛夌殑鏈€澶у€间负 49銆?/p>

绀轰緥锛?/p>

杈撳叆锛歔1,8,6,2,5,4,8,3,7]
杈撳嚭锛?9

鎬濊矾鍒嗘瀽

棣栧厛锛屾垜浠兘蹇€熸兂鍒扮殑涓€绉嶆柟娉曪細涓や袱杩涜姹傝В锛岃绠楀彲浠ユ壙杞界殑姘撮噺銆?鐒跺悗涓嶆柇鏇存柊鏈€澶у€硷紝鏈€鍚庤繑鍥炴渶澶у€煎嵆鍙€?/p>

杩欑瑙f硶锛岄渶瑕佷袱灞傚惊鐜紝鏃堕棿澶嶆潅搴︽槸O(n^2)銆傝繖绉嶇浉瀵规潵璇存瘮杈冩毚鍔涳紝瀵瑰簲灏辨槸鏆村姏娉?/code>銆?/p>

鏆村姏娉?/h4>
/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
  let max = 0;
  for (let i = 0; i < height.length - 1; i++) {
    for (let j = i + 1; j < height.length; j++) {
      let area = (j - i) * Math.min(height[i], height[j]);
      max = Math.max(max, area);
    }
  }

  return max;
};

閭d箞鏈夋病鏈夋洿濂界殑鍔炴硶鍛紵绛旀鏄偗瀹氭湁銆?/p>

鍏跺疄鏈夌偣绫讳技鍙屾寚閽?/code>鐨勬蹇碉紝宸︽寚閽堟寚鍚戜笅鏍?0锛屽彸鎸囬拡鎸囧悜length-1銆傜劧鍚庡垎鍒粠宸﹀彸涓や晶鍚戜腑闂寸Щ鍔紝姣忔鍙栧皬鐨勯偅涓€硷紙鍥犱负姘寸殑楂樺害鑲畾鏄互灏忕殑閭d釜涓哄噯锛夈€?br>

濡傛灉宸︿晶灏忎簬鍙充晶锛屽垯i++锛屽惁鍒?code>j--锛堣繖涓€姝ュ叾瀹炲氨鏄彇鎵€鏈夐珮搴︿腑姣旇緝楂樼殑锛屾垜浠煡閬撻潰绉瓑浜?code>闀?瀹?/code>锛夈€傚搴斿氨鏄?code>鍙屾寚閽?鍔ㄦ€佹粦绐?/code>

鍙屾寚閽?鍔ㄦ€佹粦绐?/h4>
/**
 * @param {number[]} height
 * @return {number}
 */
var maxArea = function (height) {
  let max = 0;
  let i = 0;
  let j = height.length - 1;
  while (i < j) {
    let minHeight = Math.min(height[i], height[j]);
    let area = (j - i) * minHeight;
    max = Math.max(max, area);
    if (height[i] < height[j]) {
      i++;
    } else {
      j--;
    }
  }
  return max;
};

鐖ゼ姊?馃帰

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏂愭尝閭e鏁板垪銆佸姩鎬佽鍒掋€?/blockquote>

棰樼洰鎻忚堪

鍋囪浣犳鍦ㄧ埇妤兼銆傞渶瑕?n聽 闃朵綘鎵嶈兘鍒拌揪妤奸《銆?/p>

姣忔浣犲彲浠ョ埇 1 鎴?2 涓彴闃躲€備綘鏈夊灏戠涓嶅悓鐨勬柟娉曞彲浠ョ埇鍒版ゼ椤跺憿锛?/p>

娉ㄦ剰锛氱粰瀹?n 鏄竴涓鏁存暟銆?/p>

绀轰緥 1锛?/p>

杈撳叆锛?2
杈撳嚭锛?2
瑙i噴锛?鏈変袱绉嶆柟娉曞彲浠ョ埇鍒版ゼ椤躲€?1.  1 闃?+ 1 闃?2.  2 闃?/code>

绀轰緥 2锛?/p>

杈撳叆锛?3
杈撳嚭锛?3
瑙i噴锛?鏈変笁绉嶆柟娉曞彲浠ョ埇鍒版ゼ椤躲€?1.  1 闃?+ 1 闃?+ 1 闃?2.  1 闃?+ 2 闃?3.  2 闃?+ 1 闃?/code>

鎬濊矾鍒嗘瀽

杩欓亾棰樼洰鏄竴閬撻潪甯搁珮棰戠殑闈㈣瘯棰樼洰锛屼篃鏄竴閬撻潪甯哥粡鍏哥殑鏂愭尝閭e鏁板垪绫诲瀷鐨勯鐩€?/p>

瑙e喅鏈亾棰樼洰鎴戜滑浼氱敤鍒板姩鎬佽鍒掔殑绠楁硶鎬濇兂-鍙互鍒嗘垚澶氫釜瀛愰棶棰橈紝鐖 n 闃舵ゼ姊殑鏂规硶鏁伴噺锛岀瓑浜?2 閮ㄥ垎涔嬪拰锛?/p>

  • 鐖笂n鈭?闃舵ゼ姊殑鏂规硶鏁伴噺銆傚洜涓哄啀鐖?1 闃跺氨鑳藉埌绗?n 闃?/li>
  • 鐖笂n鈭?闃舵ゼ姊殑鏂规硶鏁伴噺锛屽洜涓哄啀鐖?2 闃跺氨鑳藉埌绗?n 闃?br>

鍙互寰楀埌鍏紡锛?/p>

climbs[n] = climbs[n - 1] + climbs[n - 2];

鍚屾椂闇€瑕佸仛濡備笅鍒濆鍖栵細

climbs[0] = 1;
climbs[1] = 1;

浠g爜瀹炵幇

/**
 * @param {number} n
 * @return {number}
 */
var climbStairs = function (n) {
  let climbs = [];
  climbs[0] = 1;
  climbs[1] = 1;
  for (let i = 2; i <= n; i++) {
    climbs[i] = climbs[i - 1] + climbs[i - 2];
  }
  return climbs[n];
};

鐜舰閾捐〃 馃崺

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁閾捐〃銆佸揩鎱㈡寚閽堛€?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓€涓摼琛紝鍒ゆ柇閾捐〃涓槸鍚︽湁鐜€?/p>

涓轰簡琛ㄧず缁欏畾閾捐〃涓殑鐜紝鎴戜滑浣跨敤鏁存暟 pos 鏉ヨ〃绀洪摼琛ㄥ熬杩炴帴鍒伴摼琛ㄤ腑鐨勪綅缃紙绱㈠紩浠?0 寮€濮嬶級銆?濡傛灉 pos 鏄?-1锛屽垯鍦ㄨ閾捐〃涓病鏈夌幆銆?/p>

绀轰緥 1锛?/p>

杈撳叆锛歨ead = [3,2,0,-4], pos = 1
杈撳嚭锛歵rue
瑙i噴锛氶摼琛ㄤ腑鏈変竴涓幆锛屽叾灏鹃儴杩炴帴鍒扮浜屼釜鑺傜偣銆?/code>

绀轰緥 2锛?/p>

杈撳叆锛歨ead = [1,2], pos = 0
杈撳嚭锛歵rue
瑙i噴锛氶摼琛ㄤ腑鏈変竴涓幆锛屽叾灏鹃儴杩炴帴鍒扮涓€涓妭鐐广€?/code>

绀轰緥 3锛?/p>

杈撳叆锛歨ead = [1], pos = -1
杈撳嚭锛歠alse
瑙i噴锛氶摼琛ㄤ腑娌℃湁鐜€?/code>

鎬濊矾鍒嗘瀽

閾捐〃鎴愮幆闂涔熸槸闈炲父缁忓吀鐨勭畻娉曢棶棰橈紝鍦ㄩ潰璇曚腑涔熺粡甯镐細閬囧埌銆?br>

瑙e喅杩欑闂涓€鑸湁甯歌鐨勪袱绉嶆柟娉曪細鏍囧織娉?/code>鍜?code>蹇參鎸囬拡娉?/code>銆?/p>

鏍囧織娉?/h4>

缁欐瘡涓凡閬嶅巻杩囩殑鑺傜偣鍔犳爣蹇椾綅锛岄亶鍘嗛摼琛紝褰撳嚭鐜颁笅涓€涓妭鐐瑰凡琚爣蹇楁椂锛屽垯璇佹槑鍗曢摼琛ㄦ湁鐜€?/p>

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
  while (head) {
    if (head.flag) return true;
    head.flag = true;
    head = head.next;
  }
  return false;
};

蹇參鎸囬拡锛堝弻鎸囬拡娉曪級

璁剧疆蹇參涓や釜鎸囬拡锛岄亶鍘嗗崟閾捐〃锛屽揩鎸囬拡涓€娆¤蛋涓ゆ锛屾參鎸囬拡涓€娆¤蛋涓€姝ワ紝濡傛灉鍗曢摼琛ㄤ腑瀛樺湪鐜紝鍒欏揩鎱㈡寚閽堢粓浼氭寚鍚戝悓涓€涓妭鐐癸紝鍚﹀垯鐩村埌蹇寚閽堟寚鍚?code>null鏃讹紝蹇參鎸囬拡閮戒笉鍙兘鐩搁亣銆?/p>

/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * @param {ListNode} head
 * @return {boolean}
 */
var hasCycle = function (head) {
  if (!head || !head.next) {
    return false;
  }
  let slow = head,
    fast = head.next;
  while (slow !== fast) {
    if (!fast || !fast.next) return false;
    fast = fast.next.next;
    slow = slow.next;
  }
  return true;
};

鏈夋晥鐨勬嫭鍙?馃崏

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏍堛€佸搱甯岃〃銆?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓€涓彧鍖呮嫭\'(\'锛?code>\')\'锛?code>\'{\'锛?code>\'}\'锛?code>\'[\'锛?code>\']\'聽 鐨勫瓧绗︿覆锛屽垽鏂瓧绗︿覆鏄惁鏈夋晥銆?/p>

鏈夋晥瀛楃涓查渶婊¤冻锛?/p>

1銆佸乏鎷彿蹇呴』鐢ㄧ浉鍚岀被鍨嬬殑鍙虫嫭鍙烽棴鍚堛€?br>2銆佸乏鎷彿蹇呴』浠ユ纭殑椤哄簭闂悎銆?/p>

娉ㄦ剰绌哄瓧绗︿覆鍙璁や负鏄湁鏁堝瓧绗︿覆銆?/p>

绀轰緥 1:

杈撳叆: "()";
杈撳嚭: true;

绀轰緥 聽2:

杈撳叆: "()[]{}";
杈撳嚭: true;

绀轰緥 聽3:

杈撳叆: "(]";
杈撳嚭: false;

绀轰緥 聽4:

杈撳叆: "([)]";
杈撳嚭: false;

绀轰緥 聽5:

杈撳叆: "{[]}";
杈撳嚭: true;

鎬濊矾鍒嗘瀽

杩欓亾棰樺彲浠ュ埄鐢?code>鏍?/code>缁撴瀯銆?/p>

鎬濊矾澶ф鏄細閬囧埌宸︽嫭鍙凤紝涓€寰嬫帹鍏ユ爤涓紝閬囧埌鍙虫嫭鍙凤紝灏嗘爤椤堕儴鍏冪礌鎷垮嚭锛屽鏋滀笉鍖归厤鍒欒繑鍥?false锛屽鏋滃尮閰嶅垯缁х画寰幆銆?br>

绗竴绉嶈В娉曟槸鍒╃敤switch case銆?/p>

switch case

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  let arr = [];
  let len = s.length;
  if (len % 2 !== 0) return false;
  for (let i = 0; i < len; i++) {
    let letter = s[i];
    switch (letter) {
      case "(": {
        arr.push(letter);
        break;
      }
      case "{": {
        arr.push(letter);
        break;
      }
      case "[": {
        arr.push(letter);
        break;
      }
      case ")": {
        if (arr.pop() !== "(") return false;
        break;
      }
      case "}": {
        if (arr.pop() !== "{") return false;
        break;
      }
      case "]": {
        if (arr.pop() !== "[") return false;
        break;
      }
    }
  }
  return !arr.length;
};

绗簩绉嶆槸缁存姢涓€涓?code>map瀵硅薄锛?/p>

鍝堝笇琛?code>map

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function (s) {
  let map = {
    "(": ")",
    "{": "}",
    "[": "]",
  };
  let stack = [];
  let len = s.length;
  if (len % 2 !== 0) return false;
  for (let i of s) {
    if (i in map) {
      stack.push(i);
    } else {
      if (i !== map[stack.pop()]) return false;
    }
  }
  return !stack.length;
};

婊戝姩绐楀彛鏈€澶у€?鉀?/h2>
棰樼洰闅惧害hard锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鍙岀闃熷垪銆?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓€涓暟缁?nums锛屾湁涓€涓ぇ灏忎负 聽k聽 鐨勬粦鍔ㄧ獥鍙d粠鏁扮粍鐨勬渶宸︿晶绉诲姩鍒版暟缁勭殑鏈€鍙充晶銆備綘鍙彲浠ョ湅鍒板湪婊戝姩绐楀彛鍐呯殑 k聽 涓暟瀛椼€傛粦鍔ㄧ獥鍙f瘡娆″彧鍚戝彸绉诲姩涓€浣嶃€?/p>

杩斿洖婊戝姩绐楀彛涓殑鏈€澶у€笺€?/p>

杩涢樁锛氫綘鑳藉湪绾挎€ф椂闂村鏉傚害鍐呰В鍐虫棰樺悧锛?/p>

绀轰緥:

杈撳叆: nums = [1,3,-1,-3,5,3,6,7], 鍜?k = 3
杈撳嚭: [3,3,5,5,6,7]
瑙i噴:

  婊戝姩绐楀彛鐨勪綅缃?               鏈€澶у€?---------------               -----
[1  3  -1] -3  5  3  6  7       3
 1 [3  -1  -3] 5  3  6  7       3
 1  3 [-1  -3  5] 3  6  7       5
 1  3  -1 [-3  5  3] 6  7       5
 1  3  -1  -3 [5  3  6] 7       6
 1  3  -1  -3  5 [3  6  7]      7

鎻愮ず锛?/p>

  • 1 <= nums.length <= 10^5
  • -10^4 <= nums[i] <= 10^4
  • 1 <= k <= nums.length

鎬濊矾鍒嗘瀽

鏆村姏姹傝В

绗竴绉嶆柟娉曪紝姣旇緝绠€鍗曘€備篃鏄ぇ澶氭暟鍚屽寰堝揩灏辫兘鎯冲埌鐨勬柟娉曘€?/p>

  • 閬嶅巻鏁扮粍
  • 渚濇閬嶅巻姣忎釜鍖洪棿鍐呯殑鏈€澶у€硷紝鏀惧叆鏁扮粍涓?/li>
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function (nums, k) {
  let len = nums.length;
  if (len === 0) return [];
  if (k === 1) return nums;
  let resArr = [];
  for (let i = 0; i <= len - k; i++) {
    let max = Number.MIN_SAFE_INTEGER;
    for (let j = i; j < i + k; j++) {
      max = Math.max(max, nums[j]);
    }
    resArr.push(max);
  }
  return resArr;
};

鍙岀闃熷垪

杩欓亾棰樿繕鍙互鐢ㄥ弻绔槦鍒楀幓瑙e喅锛屾牳蹇冨湪浜庡湪绐楀彛鍙戠敓绉诲姩鏃讹紝鍙牴鎹彂鐢熷彉鍖栫殑鍏冪礌瀵规渶澶у€艰繘琛屾洿鏂般€?/p>

缁撳悎涓婇潰鍔ㄥ浘(鍥剧墖鏉ユ簮)鎴戜滑姊崇悊涓嬫€濊矾锛?/p>

  • 妫€鏌ラ槦灏惧厓绱狅紝鐪嬫槸涓嶆槸閮芥弧瓒冲ぇ浜庣瓑浜庡綋鍓嶅厓绱犵殑鏉′欢銆傚鏋滄槸鐨勮瘽锛岀洿鎺ュ皢褰撳墠鍏冪礌鍏ラ槦銆傚惁鍒欙紝灏嗛槦灏惧厓绱犻€愪釜鍑洪槦銆佺洿鍒伴槦灏惧厓绱犲ぇ浜庣瓑浜庡綋鍓嶅厓绱犱负姝€傦紙杩欎竴姝ユ槸涓轰簡缁存寔闃熷垪鐨勯€掑噺鎬э細纭繚闃熷ご鍏冪礌鏄綋鍓嶆粦鍔ㄧ獥鍙g殑鏈€澶у€笺€傝繖鏍锋垜浠瘡娆″彇鏈€澶у€兼椂锛岀洿鎺ュ彇闃熷ご鍏冪礌鍗冲彲銆傦級
  • 灏嗗綋鍓嶅厓绱犲叆闃?/li>
  • 妫€鏌ラ槦澶村厓绱狅紝鐪嬮槦澶村厓绱犳槸鍚﹀凡缁忚鎺掗櫎鍦ㄦ粦鍔ㄧ獥鍙g殑鑼冨洿涔嬪浜嗐€傚鏋滄槸锛屽垯灏嗛槦澶村厓绱犲嚭闃熴€傦紙杩欎竴姝ユ槸缁存寔闃熷垪鐨勬湁鏁堟€э細纭繚闃熷垪閲屾墍鏈夌殑鍏冪礌閮藉湪婊戝姩绐楀彛鍦堝畾鐨勮寖鍥翠互鍐呫€傦級
  • 鎺掗櫎鎺夋粦鍔ㄧ獥鍙h繕娌℃湁鍒濆鍖栧畬鎴愩€佺涓€涓渶澶у€艰繕娌℃湁鍑虹幇鐨勭壒娈婃儏鍐点€?/li>
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var maxSlidingWindow = function (nums, k) {
  // 缂撳瓨鏁扮粍鐨勯暱搴?  const len = nums.length;
  const res = [];
  const deque = [];
  for (let i = 0; i < len; i++) {
    // 闃熷熬鍏冪礌灏忎簬褰撳墠鍏冪礌
    while (deque.length && nums[deque[deque.length - 1]] < nums[i]) {
      deque.pop();
    }
    deque.push(i);

    // 褰撻槦澶村厓绱犵殑绱㈠紩宸茬粡琚帓闄ゅ湪婊戝姩绐楀彛涔嬪鏃?    while (deque.length && deque[0] <= i - k) {
      // 闃熷ご鍏冪礌鍑哄
      deque.shift();
    }
    if (i >= k - 1) {
      res.push(nums[deque[0]]);
    }
  }
  return res;
};

姣忔棩娓╁害 馃尅

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏍堛€?/blockquote>

棰樼洰鎻忚堪

鏍规嵁姣忔棩姘旀俯鍒楄〃锛岃閲嶆柊鐢熸垚涓€涓垪琛紝瀵瑰簲浣嶇疆鐨勮緭鍑烘槸闇€瑕佸啀绛夊緟澶氫箙娓╁害鎵嶄細鍗囬珮瓒呰繃璇ユ棩鐨勫ぉ鏁般€傚鏋滀箣鍚庨兘涓嶄細鍗囬珮锛岃鍦ㄨ浣嶇疆鐢?聽0 鏉ヤ唬鏇裤€?/p>

渚嬪锛岀粰瀹氫竴涓垪琛?聽temperatures = [73, 74, 75, 71, 69, 72, 76, 73]锛屼綘鐨勮緭鍑哄簲璇ユ槸 聽[1, 1, 4, 2, 1, 1, 0, 0]銆?/p>

鎻愮ず锛氭皵娓╁垪琛ㄩ暱搴︾殑鑼冨洿鏄?聽[1, 30000]銆傛瘡涓皵娓╃殑鍊肩殑鍧囦负鍗庢皬搴︼紝閮芥槸鍦?聽[30, 100]聽 鑼冨洿鍐呯殑鏁存暟銆?/p>

鎬濊矾鍒嗘瀽

鐪嬪埌杩欓亾棰橈紝澶у寰堝鏄撳氨浼氭兂鍒版毚鍔涢亶鍘嗘硶锛氱洿鎺ヤ袱灞傞亶鍘嗭紝绗竴灞傚畾浣嶄竴涓俯搴︼紝绗簩灞傚畾浣嶇杩欎釜娓╁害鏈€杩戠殑涓€娆″崌娓╂槸鍝ぉ锛岀劧鍚庢眰鍑轰袱涓俯搴﹀搴旂储寮曠殑宸€煎嵆鍙€?/p>

鐒惰€岃繖绉嶈В娉曢渶瑕佷袱灞傞亶鍘嗭紝鏃堕棿澶嶆潅搴︽槸O(n^2)锛屾樉鐒朵笉鏄渶浼樿В娉曘€?/p>

鏈亾棰樼洰鍙互閲囩敤鏍堝幓鍋氫竴涓紭鍖栥€?br>

澶ф鎬濊矾灏辨槸锛氱淮鎶や竴涓€掑噺鏍堛€傚綋閬嶅巻杩囩殑娓╁害锛岀淮鎸佺殑鏄竴涓崟璋冮€掑噺鐨勬€佸娍鏃讹紝鎴戜滑灏卞杩欎簺娓╁害鐨勭储寮曚笅鏍囨墽琛屽叆鏍堟搷浣滐紱鍙鍑虹幇浜嗕竴涓暟瀛楋紝瀹冩墦鐮翠簡杩欑鍗曡皟閫掑噺鐨勮秼鍔匡紝涔熷氨鏄瀹冩瘮鍓嶄竴涓俯搴﹀€奸珮锛岃繖鏃舵垜浠氨瀵瑰墠鍚庝袱涓俯搴︾殑绱㈠紩涓嬫爣姹傚樊锛屽緱鍑哄墠涓€涓俯搴﹁窛绂荤涓€娆″崌娓╃殑鐩爣宸€笺€?/p>

浠g爜瀹炵幇

/**
 * @param {number[]} T
 * @return {number[]}
 */
var dailyTemperatures = function (T) {
  const len = T.length;
  const stack = [];
  const res = new Array(len).fill(0);
  for (let i = 0; i < len; i++) {
    while (stack.length && T[i] > T[stack[stack.length - 1]]) {
      const top = stack.pop();
      res[top] = i - top;
    }
    stack.push(i);
  }
  return res;
};

鎷彿鐢熸垚 馃幆

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁閫掑綊銆佸洖婧€?/blockquote>

棰樼洰鎻忚堪

鏁板瓧 n 浠h〃鐢熸垚鎷彿鐨勫鏁帮紝璇蜂綘璁捐涓€涓嚱鏁帮紝鐢ㄤ簬鑳藉鐢熸垚鎵€鏈夊彲鑳界殑骞朵笖 鏈夋晥鐨?鎷彿缁勫悎銆?/p>

绀轰緥锛?/p>

杈撳叆锛歯 = 3
杈撳嚭锛歔
       "((()))",
       "(()())",
       "(())()",
       "()(())",
       "()()()"
     ]

鎬濊矾鍒嗘瀽

杩欓亾棰樼洰閫氳繃閫掑綊鍘诲疄鐜般€?/p>

鍥犱负宸﹀彸鎷彿闇€瑕佸尮閰嶃€侀棴鍚堛€傛墍浠ュ搴斺€?鈥濆拰鈥?鈥濈殑鏁伴噺閮芥槸n锛屽綋婊¤冻杩欎釜鏉′欢鏃讹紝涓€娆¢€掑綊灏辩粨鏉燂紝灏嗗搴斿€兼斁鍏ョ粨鏋滄暟缁勪腑銆?/p>

杩欓噷鏈変竴涓綔鍦ㄧ殑闄愬埗鏉′欢锛?code>鏈夋晥鐨?/code>鎷彿缁勫悎銆傚搴旈€昏緫灏辨槸鍦ㄥ線姣忎釜浣嶇疆鍘绘斁鍏モ€?鈥濇垨鈥?鈥濆墠锛?/p>

  • 闇€瑕佸垽鏂€?鈥濈殑鏁伴噺鏄惁灏忎簬 n
  • 鈥?鈥濈殑鏁伴噺鏄惁灏忎簬鈥?鈥?br>

浠g爜瀹炵幇

/**
 * @param {number} n
 * @return {string[]}
 */
var generateParenthesis = function (n) {
  let res = [];
  const generate = (cur, left, right) => {
    if (left === n && right === n) {
      res.push(cur);
      return;
    }
    if (left < n) {
      generate(cur + "(", left + 1, right);
    }
    if (right < left) {
      generate(cur + ")", left, right + 1);
    }
  };
  generate("", 0, 0);
  return res;
};

鐢佃瘽鍙风爜鐨勫瓧姣嶇粍鍚?馃帹

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁閫掑綊銆佸洖婧€?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓€涓粎鍖呭惈鏁板瓧 2-9 鐨勫瓧绗︿覆锛岃繑鍥炴墍鏈夊畠鑳借〃绀虹殑瀛楁瘝缁勫悎銆?/p>

缁欏嚭鏁板瓧鍒板瓧姣嶇殑鏄犲皠濡備笅锛堜笌鐢佃瘽鎸夐敭鐩稿悓锛夈€傛敞鎰?1 涓嶅搴斾换浣曞瓧姣嶃€?br>

绀轰緥:

杈撳叆锛?23"
杈撳嚭锛歔"ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].

鎬濊矾鍒嗘瀽

棣栧厛鐢ㄤ竴涓璞?code>map瀛樺偍鏁板瓧涓庡瓧姣嶇殑鏄犲皠鍏崇郴锛屾帴涓嬫潵閬嶅巻瀵瑰簲鐨勫瓧绗︿覆锛岀涓€娆″皢瀛楃涓插瓨鍦ㄧ粨鏋滄暟缁?code>result涓紝绗簩娆″強浠ュ悗鐨勫氨鍙屽眰閬嶅巻鐢熸垚鏂扮殑瀛楃涓叉暟缁勩€?br>

浠g爜瀹炵幇

鍝堝笇鏄犲皠 閫愬眰閬嶅巻

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
  let res = [];
  if (digits.length === 0) return [];
  let map = {
    2: "abc",
    3: "def",
    4: "ghi",
    5: "jkl",
    6: "mno",
    7: "pqrs",
    8: "tuv",
    9: "wxyz",
  };
  for (let num of digits) {
    let chars = map[num];
    if (res.length > 0) {
      let temp = [];
      for (let char of chars) {
        for (let oldStr of res) {
          temp.push(oldStr + char);
        }
      }
      res = temp;
    } else {
      res.push(...chars);
    }
  }
  return res;
};

閫掑綊

/**
 * @param {string} digits
 * @return {string[]}
 */
var letterCombinations = function (digits) {
  let res = [];
  if (!digits) return [];
  let map = {
    2: "abc",
    3: "def",
    4: "ghi",
    5: "jkl",
    6: "mno",
    7: "pqrs",
    8: "tuv",
    9: "wxyz",
  };
  function generate(i, str) {
    let len = digits.length;
    if (i === len) {
      res.push(str);
      return;
    }
    let chars = map[digits[i]];
    for (let j = 0; j < chars.length; j++) {
      generate(i + 1, str + chars[j]);
    }
  }
  generate(0, "");
  return res;
};

宀涘笨鏁伴噺 馃彎

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁 DFS锛堟繁搴︿紭鍏堟悳绱級銆?/blockquote>

棰樼洰鎻忚堪

缁欎綘涓€涓敱 聽\'1\'锛堥檰鍦帮級鍜?\'0\'锛堟按锛夌粍鎴愮殑鐨勪簩缁寸綉鏍硷紝璇蜂綘璁$畻缃戞牸涓矝灞跨殑鏁伴噺銆?/p>

宀涘笨鎬绘槸琚按鍖呭洿锛屽苟涓旀瘡搴у矝灞垮彧鑳界敱姘村钩鏂瑰悜鎴栫珫鐩存柟鍚戜笂鐩搁偦鐨勯檰鍦拌繛鎺ュ舰鎴愩€?/p>

姝ゅ锛屼綘鍙互鍋囪璇ョ綉鏍肩殑鍥涙潯杈瑰潎琚按鍖呭洿銆?/p>

绀轰緥 1:

杈撳叆: 11110;
11010;
11000;
00000;
杈撳嚭: 1;

绀轰緥 聽2:

杈撳叆:
11000
11000
00100
00011
杈撳嚭: 3
瑙i噴: 姣忓骇宀涘笨鍙兘鐢辨按骞冲拰/鎴栫珫鐩存柟鍚戜笂鐩搁偦鐨勯檰鍦拌繛鎺ヨ€屾垚銆?/code>

鎬濊矾鍒嗘瀽

濡備笂鍥撅紝鎴戜滑闇€瑕佽绠楃殑灏辨槸鍥句腑鐩歌繛锛堝彧鑳芥槸姘村钩鍜?鎴栫珫鐩存柟鍚戜笂鐩搁偦锛夌殑缁胯壊宀涘笨鐨勬暟閲忋€?/p>

杩欓亾棰樼洰涓€涓粡鍏哥殑鍋氭硶鏄?code>娌夊矝锛屽ぇ鑷存€濊矾鏄細閲囩敤DFS锛堟繁搴︿紭鍏堟悳绱級锛岄亣鍒?1 鐨勫氨灏嗗綋鍓嶇殑 1 鍙樹负 0锛屽苟灏嗗綋鍓嶅潗鏍囩殑涓婁笅宸﹀彸閮芥墽琛?dfs锛屽苟璁℃暟銆?/p>

缁堟鏉′欢鏄細瓒呭嚭浜岀淮鏁扮粍鐨勮竟鐣屾垨鑰呮槸閬囧埌 0 锛岀洿鎺ヨ繑鍥炪€?/p>

浠g爜瀹炵幇

/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function (grid) {
  const rows = grid.length;
  if (rows === 0) return 0;
  const cols = grid[0].length;
  let res = 0;
  for (let i = 0; i < rows; i++) {
    for (let j = 0; j < cols; j++) {
      if (grid[i][j] === "1") {
        helper(grid, i, j, rows, cols);
        res++;
      }
    }
  }
  return res;
};
function helper(grid, i, j, rows, cols) {
  if (i < 0 || j < 0 || i > rows - 1 || j > cols - 1 || grid[i][j] === "0")
    return;

  grid[i][j] = "0";

  helper(grid, i + 1, j, rows, cols);
  helper(grid, i, j + 1, rows, cols);
  helper(grid, i - 1, j, rows, cols);
  helper(grid, i, j - 1, rows, cols);
}

鍒嗗彂楗煎共 馃崻

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁璐績绠楁硶銆?/blockquote>

棰樼洰鎻忚堪

鍋囪浣犳槸涓€浣嶅緢妫掔殑瀹堕暱锛屾兂瑕佺粰浣犵殑瀛╁瓙浠竴浜涘皬楗煎共銆備絾鏄紝姣忎釜瀛╁瓙鏈€澶氬彧鑳界粰涓€鍧楅ゼ骞层€傚姣忎釜瀛╁瓙 i 锛岄兘鏈変竴涓儍鍙e€?聽gi 锛岃繖鏄兘璁╁瀛愪滑婊¤冻鑳冨彛鐨勯ゼ骞茬殑鏈€灏忓昂瀵革紱骞朵笖姣忓潡楗煎共 j 锛岄兘鏈変竴涓昂瀵?sj聽銆傚鏋?sj >= gi聽锛屾垜浠彲浠ュ皢杩欎釜楗煎共 j 鍒嗛厤缁欏瀛?i 锛岃繖涓瀛愪細寰楀埌婊¤冻銆備綘鐨勭洰鏍囨槸灏藉彲鑳芥弧瓒宠秺澶氭暟閲忕殑瀛╁瓙锛屽苟杈撳嚭杩欎釜鏈€澶ф暟鍊笺€?/p>

娉ㄦ剰锛?/b>

浣犲彲浠ュ亣璁捐儍鍙e€间负姝c€?br>涓€涓皬鏈嬪弸鏈€澶氬彧鑳芥嫢鏈変竴鍧楅ゼ骞层€?/p>

绀轰緥 聽1:

杈撳叆: [1,2,3], [1,1]

杈撳嚭: 1

瑙i噴:
浣犳湁涓変釜瀛╁瓙鍜屼袱鍧楀皬楗煎共锛?涓瀛愮殑鑳冨彛鍊煎垎鍒槸锛?,2,3銆?铏界劧浣犳湁涓ゅ潡灏忛ゼ骞诧紝鐢变簬浠栦滑鐨勫昂瀵搁兘鏄?锛屼綘鍙兘璁╄儍鍙e€兼槸1鐨勫瀛愭弧瓒炽€?鎵€浠ヤ綘搴旇杈撳嚭1銆?/code>

绀轰緥 聽2:

杈撳叆: [1,2], [1,2,3]

杈撳嚭: 2

瑙i噴:
浣犳湁涓や釜瀛╁瓙鍜屼笁鍧楀皬楗煎共锛?涓瀛愮殑鑳冨彛鍊煎垎鍒槸1,2銆?浣犳嫢鏈夌殑楗煎共鏁伴噺鍜屽昂瀵搁兘瓒充互璁╂墍鏈夊瀛愭弧瓒炽€?鎵€浠ヤ綘搴旇杈撳嚭2.

鎬濊矾鍒嗘瀽

杩欓亾棰樼洰鏄竴閬撳吀鍨嬬殑璐績绠楁硶绫汇€傝В棰樻€濊矾澶ф濡備笅锛?/p>

  • 浼樺厛婊¤冻鑳冨彛灏忕殑灏忔湅鍙嬬殑闇€姹?/li>
  • 璁炬渶澶у彲婊¤冻鐨勫瀛愭暟閲忎负maxNum = 0
  • 鑳冨彛灏忕殑鎷垮皬鐨勶紝鑳冨彛澶х殑鎷垮ぇ鐨?/li>
  • 涓よ竟鍗囧簭锛岀劧鍚庝竴涓€瀵规瘮

    • 褰?code>楗煎共j >= 鑳冨彛i 鏃讹紝i++銆?code>j++銆?code>maxNum++
    • 褰?code>楗煎共j < 鑳冨彛i鏃讹紝璇存槑楗煎共涓嶅鍚冿紝鎹㈡洿澶х殑锛?code>j++
  • 鍒拌竟鐣屽悗鍋滄

浠g爜瀹炵幇

/**
 * @param {number[]} g
 * @param {number[]} s
 * @return {number}
 */
var findContentChildren = function (g, s) {
  g = g.sort((a, b) => a - b);
  s = s.sort((a, b) => a - b);
  let gLen = g.length,
    sLen = s.length,
    i = 0,
    j = 0,
    maxNum = 0;
  while (i < gLen && j < sLen) {
    if (s[j] >= g[i]) {
      i++;
      maxNum++;
    }
    j++;
  }
  return maxNum;
};

涔板崠鑲$エ鐨勬渶浣虫椂鏈?II 馃殎

棰樼洰闅惧害easy锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鍔ㄦ€佽鍒掋€佽椽蹇冪畻娉曘€?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓€涓暟缁勶紝瀹冪殑绗?聽i 涓厓绱犳槸涓€鏀粰瀹氳偂绁ㄧ i 澶╃殑浠锋牸銆?/p>

璁捐涓€涓畻娉曟潵璁$畻浣犳墍鑳借幏鍙栫殑鏈€澶у埄娑︺€備綘鍙互灏藉彲鑳藉湴瀹屾垚鏇村鐨勪氦鏄擄紙澶氭涔板崠涓€鏀偂绁級銆?/p>

娉ㄦ剰锛?/b>浣犱笉鑳藉悓鏃跺弬涓庡绗斾氦鏄擄紙浣犲繀椤诲湪鍐嶆璐拱鍓嶅嚭鍞帀涔嬪墠鐨勮偂绁級銆?/p>

绀轰緥 1:

杈撳叆: [7,1,5,3,6,4]
杈撳嚭: 7
瑙i噴: 鍦ㄧ 2 澶╋紙鑲$エ浠锋牸 = 1锛夌殑鏃跺€欎拱鍏ワ紝鍦ㄧ 3 澶╋紙鑲$エ浠锋牸 = 5锛夌殑鏃跺€欏崠鍑? 杩欑瑪浜ゆ槗鎵€鑳借幏寰楀埄娑?= 5-1 = 4 銆?聽    闅忓悗锛屽湪绗?4 澶╋紙鑲$エ浠锋牸 = 3锛夌殑鏃跺€欎拱鍏ワ紝鍦ㄧ 5 澶╋紙鑲$エ浠锋牸 = 6锛夌殑鏃跺€欏崠鍑? 杩欑瑪浜ゆ槗鎵€鑳借幏寰楀埄娑?= 6-3 = 3 銆?/code>

绀轰緥 2:

杈撳叆: [1,2,3,4,5]
杈撳嚭: 4
瑙i噴: 鍦ㄧ 1 澶╋紙鑲$エ浠锋牸 = 1锛夌殑鏃跺€欎拱鍏ワ紝鍦ㄧ 5 澶?锛堣偂绁ㄤ环鏍?= 5锛夌殑鏃跺€欏崠鍑? 杩欑瑪浜ゆ槗鎵€鑳借幏寰楀埄娑?= 5-1 = 4 銆?聽    娉ㄦ剰浣犱笉鑳藉湪绗?1 澶╁拰绗?2 澶╂帴杩炶喘涔拌偂绁紝涔嬪悗鍐嶅皢瀹冧滑鍗栧嚭銆?聽    鍥犱负杩欐牱灞炰簬鍚屾椂鍙備笌浜嗗绗斾氦鏄擄紝浣犲繀椤诲湪鍐嶆璐拱鍓嶅嚭鍞帀涔嬪墠鐨勮偂绁ㄣ€?/code>

绀轰緥 聽3:

杈撳叆: [7,6,4,3,1]
杈撳嚭: 0
瑙i噴: 鍦ㄨ繖绉嶆儏鍐典笅, 娌℃湁浜ゆ槗瀹屾垚, 鎵€浠ユ渶澶у埄娑︿负 0銆?/code>

鎻愮ず锛?/b>

  • 1 <= prices.length <= 3 * 10 ^ 4
  • 0 <= prices[i]聽<= 10 ^ 4

鎬濊矾鍒嗘瀽

鍏跺疄杩欓亾棰樼洰鎬濊矾涔熸瘮杈冪畝鍗曪細

  • 缁存姢涓€涓彉閲?code>profit鐢ㄦ潵瀛樺偍鍒╂鼎
  • 鍥犱负鍙互澶氭涔板崠锛岄偅涔堝氨瑕佸悗闈㈢殑浠锋牸姣斿墠闈㈢殑澶э紝閭d箞灏卞彲浠ヨ繘琛屼拱鍗?/li>
  • 鍥犳锛屽彧瑕?code>prices[i+1] > prices[i]锛岄偅涔堝氨鍘诲彔鍔?code>profit
  • 閬嶅巻瀹屾垚寰楀埌鐨?code>profit灏辨槸鑾峰彇鐨勬渶澶у埄娑?br>

浠g爜瀹炵幇

/**
 * @param {number[]} prices
 * @return {number}
 */
var maxProfit = function (prices) {
  let profit = 0;
  for (let i = 0; i < prices.length - 1; i++) {
    if (prices[i + 1] > prices[i]) profit += prices[i + 1] - prices[i];
  }
  return profit;
};

涓嶅悓璺緞 馃洠

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鍔ㄦ€佽鍒掋€?/blockquote>

棰樼洰鎻忚堪

涓€涓満鍣ㄤ汉浣嶄簬涓€涓?m x n 缃戞牸鐨勫乏涓婅 锛堣捣濮嬬偣鍦ㄤ笅鍥句腑鏍囪涓衡€淪tart鈥?锛夈€?/p>

鏈哄櫒浜烘瘡娆″彧鑳藉悜涓嬫垨鑰呭悜鍙崇Щ鍔ㄤ竴姝ャ€傛満鍣ㄤ汉璇曞浘杈惧埌缃戞牸鐨勫彸涓嬭锛堝湪涓嬪浘涓爣璁颁负鈥淔inish鈥濓級銆?/p>

闂€诲叡鏈夊灏戞潯涓嶅悓鐨勮矾寰勶紵

渚嬪锛屼笂鍥炬槸涓€涓?7 x 3 鐨勭綉鏍笺€傛湁澶氬皯鍙兘鐨勮矾寰勶紵

绀轰緥 聽1:

杈撳叆: m = 3, n = 2
杈撳嚭: 3
瑙i噴:
浠庡乏涓婅寮€濮嬶紝鎬诲叡鏈?3 鏉¤矾寰勫彲浠ュ埌杈惧彸涓嬭銆?1. 鍚戝彸 -> 鍚戝彸 -> 鍚戜笅
2. 鍚戝彸 -> 鍚戜笅 -> 鍚戝彸
3. 鍚戜笅 -> 鍚戝彸 -> 鍚戝彸

绀轰緥 聽2:

杈撳叆: (m = 7), (n = 3);
杈撳嚭: 28;

鎬濊矾鍒嗘瀽

鐢遍鍙煡锛氭満鍣ㄤ汉鍙兘鍚戝彸鎴栧悜涓嬬Щ鍔ㄤ竴姝ワ紝閭d箞浠庡乏涓婅鍒板彸涓嬭鐨勮蛋娉?= 浠庡彸杈瑰紑濮嬭蛋鐨勮矾寰勬€绘暟+浠庝笅杈瑰紑濮嬭蛋鐨勮矾寰勬€绘暟銆?br>

鎵€浠ュ彲鎺ㄥ嚭鍔ㄦ€佹柟绋嬩负锛?code>dp[i][j] = dp[i-1][j]+dp[i][j-1]銆?/p>

浠g爜瀹炵幇

杩欓噷閲囩敤Array(m).fill(Array(n).fill(1))杩涜浜嗗垵濮嬪寲锛屽洜涓烘瘡涓€鏍艰嚦灏戞湁涓€绉嶈蛋娉曘€?/blockquote>
/**
 * @param {number} m
 * @param {number} n
 * @return {number}
 */
var uniquePaths = function (m, n) {
  let dp = Array(m).fill(Array(n).fill(1));
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
    }
  }
  return dp[m - 1][n - 1];
};

闆堕挶鍏戞崲 馃挵

棰樼洰闅惧害medium锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鍔ㄦ€佽鍒掋€?/blockquote>

棰樼洰鎻忚堪

缁欏畾涓嶅悓闈㈤鐨勭‖甯?coins 鍜屼竴涓€婚噾棰?amount銆傜紪鍐欎竴涓嚱鏁版潵璁$畻鍙互鍑戞垚鎬婚噾棰濇墍闇€鐨勬渶灏戠殑纭竵涓暟銆傚鏋滄病鏈変换浣曚竴绉嶇‖甯佺粍鍚堣兘缁勬垚鎬婚噾棰濓紝杩斿洖 聽-1銆?/p>

绀轰緥 聽1:

杈撳叆: (coins = [1, 2, 5]), (amount = 11);
杈撳嚭: 3;
瑙i噴: 11 = 5 + 5 + 1;

绀轰緥 2:

杈撳叆: (coins = [2]), (amount = 3);
杈撳嚭: -1;

璇存槑:
浣犲彲浠ヨ涓烘瘡绉嶇‖甯佺殑鏁伴噺鏄棤闄愮殑銆?/p>

鎬濊矾鍒嗘瀽

杩欓亾棰樼洰鎴戜滑鍚屾牱閲囩敤鍔ㄦ€佽鍒?/code>鏉ヨВ鍐炽€?br>

鍋囪缁欏嚭鐨勪笉鍚岄潰棰濈殑纭竵鏄痆1, 2, 5]锛岀洰鏍囨槸 60锛岄棶鏈€灏戦渶瑕佺殑纭竵涓暟锛?/p>

鎴戜滑闇€瑕佸厛鍒嗚В瀛愰棶棰橈紝鍒嗗眰绾ф壘鏈€浼樺瓙缁撴瀯銆?/p>

dp[i]: 琛ㄧず鎬婚噾棰濅负 i 鐨勬椂鍊欐渶浼樿В娉曠殑纭竵鏁?/blockquote>

鎴戜滑鎯充竴涓嬶細姹傛€婚噾棰?60 鏈夊嚑绉嶆柟娉曪紵涓€鍏辨湁 3 绉嶆柟寮忥紝鍥犱负鎴戜滑鏈?3 绉嶄笉鍚岄潰鍊肩殑纭竵銆?/p>

  • 鎷夸竴鏋氶潰鍊间负 1 鐨勭‖甯?+ 鎬婚噾棰濅负 59 鐨勬渶浼樿В娉曠殑纭竵鏁伴噺銆傚嵆锛歞p[59] + 1
  • 鎷夸竴鏋氶潰鍊间负 2 鐨勭‖甯?+ 鎬婚噾棰濅负 58 鐨勬渶浼樿В娉曠殑纭竵鏁般€傚嵆锛歞p[58] + 1
  • 鎷夸竴鏋氶潰鍊间负 5 鐨勭‖甯?+ 鎬婚噾棰濅负 55 鐨勬渶浼樿В娉曠殑纭竵鏁般€傚嵆锛歞p[55] + 1

鎵€浠ワ紝鎬婚噾棰濅负 60 鐨勬渶浼樿В娉曞氨鏄笂闈㈣繖涓夌瑙f硶涓渶浼樼殑涓€绉嶏紝涔熷氨鏄‖甯佹暟鏈€灏戠殑涓€绉嶏紝鎴戜滑涓嬮潰鐢ㄤ唬鐮佹潵琛ㄧず涓€涓嬶細

dp[60] = Math.min(dp[59] + 1, dp[58] + 1, dp[55] + 1);

鎺ㄥ鍑?code>鐘舵€佽浆绉绘柟绋?/code>锛?/p>

dp[i] = Math.min(dp[i - coin] + 1, dp[i - coin] + 1, ...)
鍏朵腑 coin 鏈夊灏戠鍙兘锛屾垜浠氨闇€瑕佹瘮杈冨灏戞锛岄亶鍘?coins 鏁扮粍锛屽垎鍒幓瀵规瘮鍗冲彲

浠g爜瀹炵幇

/**
 * @param {number[]} coins
 * @param {number} amount
 * @return {number}
 */
var coinChange = function (coins, amount) {
  let dp = new Array(amount + 1).fill(Infinity);
  dp[0] = 0;
  for (let i = 0; i <= amount; i++) {
    for (let coin of coins) {
      if (i - coin >= 0) {
        dp[i] = Math.min(dp[i], dp[i - coin] + 1);
      }
    }
  }
  return dp[amount] === Infinity ? -1 : dp[amount];
};

绂忓埄

澶у鏁板墠绔悓瀛﹀浜庣畻娉曠殑绯荤粺瀛︿範锛屽叾瀹炴槸姣旇緝鑼劧鐨勶紝杩欓噷鎴戞暣鐞嗕簡涓€寮犳€濈淮瀵煎浘锛岀畻鏄瘮杈冨叏闈㈢殑姒傛嫭浜嗗墠绔畻娉曚綋绯汇€?br>

鍙﹀鎴戣繕缁存姢浜嗕竴涓?code>github浠撳簱锛?code>https://github.com/Cosen95/js_algorithm锛岄噷闈㈠寘鍚簡澶ч噺鐨?code>leetcode棰樿В锛屽苟涓旇繕鍦ㄤ笉鏂洿鏂颁腑锛屾劅瑙変笉閿欑殑缁欎釜star鍝堬紒馃

鉂わ笍 鐖卞績涓夎繛鍑?/h2>

1.濡傛灉瑙夊緱杩欑瘒鏂囩珷杩樹笉閿欙紝鏉ヤ釜鍒嗕韩銆佺偣璧炪€佸湪鐪?/b>涓夎繛鍚э紝璁╂洿澶氱殑浜轰篃鐪嬪埌锝?/p>

2.鍏虫敞鍏紬鍙?b>鍓嶇妫灄锛屽畾鏈熶负浣犳帹閫佹柊椴滃共璐уソ鏂囥€?/p>

3.鐗规畩闃舵锛屽甫濂藉彛缃╋紝鍋氬ソ涓汉闃叉姢銆?br>

以上是关于銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫?/a>的主要内容,如果未能解决你的问题,请参考以下文章

闈㈣瘯瀹橀棶锛氫綘浜嗚ВHTTP2.0鍚楋紵

銆恖eetcode銆戦潰璇曢 08.10. 棰滆壊濉厖

涓€鏂囧甫浣犱簡瑙ue涔嬭櫄鎷焏om

涓轰粈涔堥潰璇曞畼鍠滄闂?ZooKeeper 鍩烘湰鍘熺悊锛?/h1>

骞茶揣锛屾帴鍙f祴璇曢潰璇曢