銆岄潰璇曞繀闂€峫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硶锛岄渶瑕佷袱灞傚惊鐜紝鏃堕棿澶嶆潅搴︽槸 閭d箞鏈夋病鏈夋洿濂界殑鍔炴硶鍛紵绛旀鏄偗瀹氭湁銆?/p> 鍏跺疄鏈夌偣绫讳技 濡傛灉宸︿晶灏忎簬鍙充晶锛屽垯O(n^2)
銆傝繖绉嶇浉瀵规潵璇存瘮杈冩毚鍔涳紝瀵瑰簲灏辨槸鏆村姏娉?/code>銆?/p>
锛堣繖涓€姝ュ叾瀹炲氨鏄彇鎵€鏈夐珮搴︿腑姣旇緝楂樼殑锛屾垜浠煡閬撻潰绉瓑浜?code>闀?瀹?/code>锛夈€傚搴斿氨鏄?code>鍙屾寚閽?鍔ㄦ€佹粦绐?/code>鏆村姏娉?/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;
};
鍙屾寚閽?/code>鐨勬蹇碉紝宸︽寚閽堟寚鍚戜笅鏍?0锛屽彸鎸囬拡鎸囧悜
length-1
銆傜劧鍚庡垎鍒粠宸﹀彸涓や晶鍚戜腑闂寸Щ鍔紝姣忔鍙栧皬鐨勯偅涓€硷紙鍥犱负姘寸殑楂樺害鑲畾鏄互灏忕殑閭d釜涓哄噯锛夈€?br>i++
锛屽惁鍒?code>j--
鍙屾寚閽?鍔ㄦ€佹粦绐?/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>的主要内容,如果未能解决你的问题,请参考以下文章
/**
* @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>
鏃讹紝蹇參鎸囬拡閮戒笉鍙兘鐩搁亣銆?/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
/** * 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>的主要内容,如果未能解决你的问题,请参考以下文章