Posted 鍓嶇妫灄
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
寮曡█锛堟枃鏈湁绂忓埄锛夝煆?/span>
绠楁硶涓€鐩存槸澶у巶鍓嶇闈㈣瘯甯搁棶鐨勪竴鍧楋紝鑰屽ぇ瀹跺線寰€鍑嗗杩欐柟闈㈢殑闈㈣瘯閮芥槸閫氳繃leetcode
鍒烽銆?/p>
鎴戠壒鍦版暣鐞嗕簡鍑犻亾leetcode
涓€?code class="mq-8">寰堟湁鎰忔€?/code>銆嶈€屼笖闈炲父銆?code class="mq-9">楂橀銆嶇殑绠楁硶棰樼洰锛屽垎鍒粰鍑轰簡鎬濊矾鍒嗘瀽锛堝甫鍥捐В锛夊拰浠g爜瀹炵幇銆?/p>
璁ょ湡浠旂粏鐨勯槄璇诲畬鏈枃锛岀浉淇″浜庝綘鍦ㄧ畻娉曟柟闈㈢殑闈㈣瘯涓€瀹氫細鏈変笉灏忕殑甯姪锛侌煠?/p>
涓ゆ暟涔嬪拰 馃
鉂?/span>棰樼洰闅惧害
鉂?/span>easy
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸搱甯岃〃
棰樼洰鎻忚堪
缁欏畾涓€涓暣鏁版暟缁?nums
鍜屼竴涓洰鏍囧€?target
锛岃浣犲湪璇ユ暟缁勪腑鎵惧嚭鍜屼负鐩爣鍊肩殑閭?code class="mq-26">涓や釜鏁存暟锛屽苟杩斿洖浠栦滑鐨勬暟缁勪笅鏍囥€?/p>
浣犲彲浠ュ亣璁炬瘡绉嶈緭鍏ュ彧浼氬搴斾竴涓瓟妗堛€備絾鏄紝鏁扮粍涓悓涓€涓厓绱犱笉鑳戒娇鐢ㄤ袱閬嶃€?/p>
绀轰緥锛?/p>
缁欏畾 nums = [2, 7, 11, 15], target = 9
鍥犱负 nums[0] + nums[1] = 2 + 7 = 9
鎵€浠ヨ繑鍥?nbsp;[0, 1]
鎬濊矾鍒嗘瀽
澶у鏁板悓瀛︾湅鍒拌繖閬撻鐩紝蹇冧腑鑲畾浼氭兂锛氳繖閬撻鐩お绠€鍗曚簡锛屼笉灏变袱灞傞亶鍘嗗槢锛氫袱灞傚惊鐜潵閬嶅巻鍚屼竴涓暟缁勶紱绗竴灞傚惊鐜亶鍘嗙殑鍊艰涓?code class="mq-48">a锛岀浜屽眰寰幆鏃堕亶鍘嗙殑鍊艰涓?code class="mq-49">b锛涜嫢a+b = 鐩爣鍊?/code>锛岄偅涔?code class="mq-51">a
鍜?code class="mq-52">b瀵瑰簲鐨勬暟缁勪笅鏍囧氨鏄垜浠兂瑕佺殑绛旀銆?/p>
杩欑瑙f硶娌℃瘺鐥咃紝浣嗘湁娌℃湁浼樺寲鐨勬柟妗堝憿锛燄煠?/p>
瑕佺煡閬撲袱灞傚惊鐜緢澶氭儏鍐典笅閮芥剰鍛崇潃O(n^2)
鐨勫鏉傚害锛岃繖涓鏉傚害闈炲父瀹规槗瀵艰嚧浣犵殑绠楁硶瓒呮椂銆傚嵆渚挎病鏈夎秴鏃讹紝鍦ㄦ槑鏄庢湁涓€灞傞亶鍘嗚В娉曠殑鎯呭喌涓嬶紝浣犲啓浜嗕袱灞傞亶鍘嗭紝闈㈣瘯瀹樹篃浼氬浣犵殑鍗拌薄鍒嗗ぇ鎵撴姌鎵c€傪煠?/p>
鍏跺疄鎴戜滑鍙互鍦ㄩ亶鍘嗘暟缁勭殑杩囩▼涓紝澧炲姞涓€涓?code class="mq-57">Map缁撴瀯鏉ュ瓨鍌ㄥ凡缁忛亶鍘嗚繃鐨勬暟瀛楀強鍏跺搴旂殑绱㈠紩鍊笺€傜劧鍚庢瘡閬嶅巻鍒颁竴涓柊鏁板瓧鐨勬椂鍊欙紝閮藉洖鍒?code class="mq-58">Map閲屽幓鏌ヨtargetNum
涓庤鏁扮殑宸€兼槸鍚﹀凡缁忓湪鍓嶉潰鐨勬暟瀛椾腑鍑虹幇杩囦簡銆傝嫢鍑虹幇杩囷紝閭d箞绛旀宸茬劧鏄剧幇锛屾垜浠氨涓嶅繀鍐嶅線涓嬭蛋浜嗐€?/p>
鎴戜滑灏变互鏈涓殑渚嬪瓙缁撳悎鍥剧墖鏉ヨ鏄庝竴涓嬩笂闈㈡彁鍒扮殑杩欑鎬濊矾锛?/p>
-
杩欓噷鐢ㄥ璞? diffs
鏉ユā鎷?map
缁撴瀯锛?棣栧厛閬嶅巻鏁扮粍绗竴涓厓绱狅紝姝ゆ椂key
涓?2锛?value
涓虹储寮?0 -
寰€涓嬮亶鍘嗭紝閬囧埌浜?7: 璁$畻 targetNum
鍜?7 鐨勫樊鍊间负 2锛屽幓diffs
涓绱?2 杩欎釜key
锛屽彂鐜版槸涔嬪墠鍑虹幇杩囩殑鍊笺€傞偅涔堟湰棰樼殑绛旀灏卞嚭鏉ヤ簡锛?
浠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 宸€兼槸鍚﹀瓨鍦?/span>
if (diffs[target - nums[i]] !== undefined) {
// 鑻ユ湁瀵瑰簲宸€硷紝閭d箞寰楀埌绛旀
return [diffs[target - nums[i]], i];
}
// 鑻ユ病鏈夊搴斿樊鍊硷紝鍒欒褰曞綋鍓嶅€?/span>
diffs[nums[i]] = i;
}
};
涓夋暟涔嬪拰 馃
鉂?/span>棰樼洰闅惧害
鉂?/span>medium
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸弻鎸囬拡
棰樼洰鎻忚堪
缁欎綘涓€涓寘鍚?code class="mq-109">n涓暣鏁扮殑鏁扮粍nums
锛屽垽鏂?code class="mq-111">nums涓槸鍚﹀瓨鍦ㄤ笁涓厓绱?code class="mq-112">a锛?code class="mq-113">b锛?code class="mq-114">c 锛屼娇寰?code class="mq-115">a + b + c = 0銆傝浣犳壘鍑烘墍鏈夋弧瓒虫潯浠朵笖涓嶉噸澶嶇殑涓夊厓缁勩€?/p>
娉ㄦ剰锛氱瓟妗堜腑涓嶅彲浠ュ寘鍚噸澶嶇殑涓夊厓缁勩€?/p>
绀轰緥锛?/p>
缁欏畾鏁扮粍 nums = [-1, 0, 1, 2, -1, -4]锛?br>
婊¤冻瑕佹眰鐨勪笁鍏冪粍闆嗗悎涓猴細
[
[-1, 0, 1],
[-1, -1, 2]
]
鎬濊矾鍒嗘瀽
鍜屼笂闈㈢殑涓ゆ暟涔嬪拰
涓€鏍凤紝濡傛灉涓嶈鐪熸€濊€冿紝鏈€蹇殑鏂瑰紡鍙兘灏辨槸澶氬眰閬嶅巻浜嗐€備絾鏈変簡鍓嶈溅涔嬮壌锛屾垜浠悓鏍峰彲浠ユ妸姹傚拰闂鍙樹负姹傚樊闂锛氬浐瀹氬叾涓竴涓暟锛屽湪鍓╀笅鐨勬暟涓鎵炬槸鍚︽湁涓や釜鏁扮殑鍜岃繖涓浐瀹氭暟鐩稿姞鏄瓑浜?0 鐨勩€?/p>
杩欓噷鎴戜滑閲囩敤鍙屾寚閽堟硶
鏉ヨВ鍐抽棶棰橈紝鐩告瘮涓夊眰寰幆锛屾晥鐜囦細澶уぇ鎻愬崌銆?/p>
鉂?/span>鍙屾寚閽堟硶鐨勯€傜敤鑼冨洿姣旇緝骞匡紝涓€鑸儚姹傚拰銆佹瘮澶у皬鐨勯兘鍙互鐢ㄥ畠鏉ヨВ鍐炽€備絾鏄湁涓€涓墠鎻愶細鏁扮粍蹇呴』鏈夊簭
鉂?/span>
鍥犳鎴戜滑鐨勭涓€姝ュ氨鏄厛灏嗘暟缁勮繘琛屾帓搴忥細
// 缁?nbsp;nums 鎺掑簭
nums = nums.sort((a,b)=>{
return a-b
})
鐒跺悗瀵规暟缁勮繘琛岄亶鍘嗭紝姣忛亶鍘嗗埌鍝釜鏁板瓧锛屽氨鍥哄畾褰撳墠鐨勬暟瀛椼€傚悓鏃跺乏鎸囬拡鎸囧悜璇ユ暟瀛楀悗闈㈢殑绱ч偦鐨勯偅涓暟瀛楋紝鍙虫寚閽堟寚鍚戞暟缁勬湯灏俱€傜劧鍚庡乏鍙虫寚閽堝垎鍒悜涓棿闈犳嫝锛?img data-ratio="0.5138705416116248" src="/img?url=https://mmbiz.qpic.cn/mmbiz_png/LNrWl4n5XIKtv5uplOwZ663ZIcCoriaYtVC7lK2g3Uwxib6Swyrib0TYFOGxvZVDum53SQ4FkgWxZzPKU6vzhic6Aw/640?wx_fmt=png" data-type="png" data-w="1514" alt="銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫? class="mq-152">姣忔鎸囬拡绉诲姩涓€娆′綅缃紝灏辫绠椾竴涓嬩袱涓寚閽堟寚鍚戞暟瀛椾箣鍜屽姞涓婂浐瀹氱殑閭d釜鏁颁箣鍚庯紝鏄惁绛変簬 0銆傚鏋滄槸锛岄偅涔堟垜浠氨寰楀埌浜嗕竴涓洰鏍囩粍鍚堬紱鍚﹀垯锛屽垎涓ょ鎯呭喌鏉ョ湅锛?/p>
-
鐩稿姞涔嬪拰澶т簬 0锛岃鏄庡彸渚х殑鏁板亸澶т簡锛屽彸鎸囬拡宸︾Щ -
鐩稿姞涔嬪拰灏忎簬 0锛岃鏄庡乏渚х殑鏁板亸灏忎簡锛屽乏鎸囬拡鍙崇Щ
浠g爜瀹炵幇
/**
* @param {number[]} nums
* @return {number[][]}
*/
const threeSum = function(nums) {
// 鐢ㄤ簬瀛樻斁缁撴灉鏁扮粍
let res = []
// 鐩爣鍊间负0
let sum = 0
// 缁?nbsp;nums 鎺掑簭
nums = nums.sort((a,b)=>{
return a-b
})
// 缂撳瓨鏁扮粍闀垮害
const len = nums.length
for(let i=0;i<len-2;i++) {
// 宸︽寚閽?nbsp;j
let j=i+1
// 鍙虫寚閽坘
let k=len-1
// 濡傛灉閬囧埌閲嶅鐨勬暟瀛楋紝鍒欒烦杩?/span>
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 {
// 寰楀埌鐩爣鏁板瓧缁勫悎锛屾帹鍏ョ粨鏋滄暟缁?/span>
res.push([nums[i],nums[j],nums[k]])
// 宸﹀彸鎸囬拡涓€璧峰墠杩?/span>
j++
k--
// 鑻ュ乏鎸囬拡鍏冪礌閲嶅锛岃烦杩?/span>
while(j<k&&nums[j]===nums[j-1]) {
j++
}
// 鑻ュ彸鎸囬拡鍏冪礌閲嶅锛岃烦杩?/span>
while(j<k&&nums[k]===nums[k+1]) {
k--
}
}
}
}
// 杩斿洖缁撴灉鏁扮粍
return res
};
鐩涙渶澶氭按鐨勫鍣?馃
鉂?/span>棰樼洰闅惧害
鉂?/span>medium
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏁扮粍銆佸弻鎸囬拡
棰樼洰鎻忚堪
缁欎綘 n 涓潪璐熸暣鏁?a1锛宎2锛?..锛宎n锛屾瘡涓暟浠h〃鍧愭爣涓殑涓€涓偣 (i, ai) 銆傚湪鍧愭爣鍐呯敾 n 鏉″瀭鐩寸嚎锛屽瀭鐩寸嚎 i 鐨勪袱涓鐐瑰垎鍒负 (i, ai) 鍜?(i, 0)銆傛壘鍑哄叾涓殑涓ゆ潯绾匡紝浣垮緱瀹冧滑涓? x 杞村叡鍚屾瀯鎴愮殑瀹瑰櫒鍙互瀹圭撼鏈€澶氱殑姘淬€?/p>
璇存槑锛氫綘涓嶈兘鍊炬枩瀹瑰櫒锛屼笖 n 鐨勫€艰嚦灏戜负 2銆?img data-ratio="0.4781523096129838" src="/img?url=https://mmbiz.qpic.cn/mmbiz_jpg/LNrWl4n5XIKtv5uplOwZ663ZIcCoriaYt2Eib90FHNwe36tHvQvYm35DnBquFdK5Xygr72qj7EmtnelvDRoHoTew/640?wx_fmt=jpeg" data-type="jpeg" data-w="801" alt="銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫? class="mq-232">鍥句腑鍨傜洿绾夸唬琛ㄨ緭鍏ユ暟缁刐1,8,6,2,5,4,8,3,7]銆傚湪姝ゆ儏鍐典笅锛屽鍣ㄨ兘澶熷绾虫按锛堣〃绀轰负钃濊壊閮ㄥ垎锛夌殑鏈€澶у€间负 49銆?/p>
绀轰緥锛?/p>
杈撳叆锛歔1,8,6,2,5,4,8,3,7]
杈撳嚭锛?span class="mq-245">49
鎬濊矾鍒嗘瀽
棣栧厛锛屾垜浠兘蹇€熸兂鍒扮殑涓€绉嶆柟娉曪細涓や袱杩涜姹傝В锛岃绠楀彲浠ユ壙杞界殑姘撮噺銆?鐒跺悗涓嶆柇鏇存柊鏈€澶у€硷紝鏈€鍚庤繑鍥炴渶澶у€煎嵆鍙€?/p>
杩欑瑙f硶锛岄渶瑕佷袱灞傚惊鐜紝鏃堕棿澶嶆潅搴︽槸 閭d箞鏈夋病鏈夋洿濂界殑鍔炴硶鍛紵绛旀鏄偗瀹氭湁銆?/p>
鍏跺疄鏈夌偣绫讳技 濡傛灉宸︿晶灏忎簬鍙充晶锛屽垯O(n^2)
銆傝繖绉嶇浉瀵规潵璇存瘮杈冩毚鍔涳紝瀵瑰簲灏辨槸鏆村姏娉?/code>銆?/p>
锛堣繖涓€姝ュ叾瀹炲氨鏄彇鎵€鏈夐珮搴︿腑姣旇緝楂樼殑锛屾垜浠煡閬撻潰绉瓑浜?code class="mq-285">闀?瀹?/code>锛夈€傚搴斿氨鏄?code class="mq-286">鍙屾寚閽?鍔ㄦ€佹粦绐?/code>鏆村姏娉?span class="mq-256">
/**
* @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釜涓哄噯锛夈€?img data-ratio="1.0056818181818181" src="/img?url=https://mmbiz.qpic.cn/mmbiz_png/LNrWl4n5XIKtv5uplOwZ663ZIcCoriaYtNsfnZBjN9Paict7rnqia47vbsMx2gQtEXh05xr3gpJjphmIXhSibIz31g/640?wx_fmt=png" data-type="png" data-w="1408" alt="銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫? class="mq-281">i++
锛屽惁鍒?code class="mq-284">j--
鍙屾寚閽?鍔ㄦ€佹粦绐?span class="mq-289">
/**
* @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
};
鐖ゼ姊?馃帰
鉂?/span>棰樼洰闅惧害
easy
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏂愭尝閭e鏁板垪銆佸姩鎬佽鍒掋€?/p> 鉂?/span>
棰樼洰鎻忚堪
鍋囪浣犳鍦ㄧ埇妤兼銆傞渶瑕?n 闃朵綘鎵嶈兘鍒拌揪妤奸《銆?/p>
姣忔浣犲彲浠ョ埇 1 鎴?2 涓彴闃躲€備綘鏈夊灏戠涓嶅悓鐨勬柟娉曞彲浠ョ埇鍒版ゼ椤跺憿锛?/p>
娉ㄦ剰锛氱粰瀹?n 鏄竴涓鏁存暟銆?/p>
绀轰緥 1锛?/p>
杈撳叆锛?nbsp;2
杈撳嚭锛?nbsp;2
瑙i噴锛?nbsp;鏈変袱绉嶆柟娉曞彲浠ョ埇鍒版ゼ椤躲€?br>1. 1 闃?nbsp;+ 1 闃?br>2. 2 闃?br>
绀轰緥 2锛?/p>
杈撳叆锛?nbsp;3
杈撳嚭锛?nbsp;3
瑙i噴锛?nbsp;鏈変笁绉嶆柟娉曞彲浠ョ埇鍒版ゼ椤躲€?br>1. 1 闃?nbsp;+ 1 闃?nbsp;+ 1 闃?br>2. 1 闃?nbsp;+ 2 闃?br>3. 2 闃?nbsp;+ 1 闃?br>
鎬濊矾鍒嗘瀽
杩欓亾棰樼洰鏄竴閬撻潪甯搁珮棰戠殑闈㈣瘯棰樼洰锛屼篃鏄竴閬撻潪甯哥粡鍏哥殑鏂愭尝閭e鏁板垪
绫诲瀷鐨勯鐩€?/p>
瑙e喅鏈亾棰樼洰鎴戜滑浼氱敤鍒板姩鎬佽鍒掔殑绠楁硶鎬濇兂-鍙互鍒嗘垚澶氫釜瀛愰棶棰橈紝鐖 n 闃舵ゼ姊殑鏂规硶鏁伴噺锛岀瓑浜?2 閮ㄥ垎涔嬪拰锛?/p>
-
鐖笂 n鈭?
闃舵ゼ姊殑鏂规硶鏁伴噺銆傚洜涓哄啀鐖?1 闃跺氨鑳藉埌绗?n 闃? -
鐖笂 n鈭?
闃舵ゼ姊殑鏂规硶鏁伴噺锛屽洜涓哄啀鐖?2 闃跺氨鑳藉埌绗?n 闃?
鍙互寰楀埌鍏紡锛?/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]
};
鐜舰閾捐〃 馃崺
鉂?/span>棰樼洰闅惧害
easy
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁閾捐〃銆佸揩鎱㈡寚閽堛€?/p> 鉂?/span>
棰樼洰鎻忚堪
缁欏畾涓€涓摼琛紝鍒ゆ柇閾捐〃涓槸鍚︽湁鐜€?/p>
涓轰簡琛ㄧず缁欏畾閾捐〃涓殑鐜紝鎴戜滑浣跨敤鏁存暟 pos 鏉ヨ〃绀洪摼琛ㄥ熬杩炴帴鍒伴摼琛ㄤ腑鐨勪綅缃紙绱㈠紩浠?0 寮€濮嬶級銆?濡傛灉 pos 鏄?-1锛屽垯鍦ㄨ閾捐〃涓病鏈夌幆銆?/p>
绀轰緥 1锛?/p>
杈撳叆锛歨ead = [3,2,0,-4], pos = 1
杈撳嚭锛?span class="mq-420">true
瑙i噴锛氶摼琛ㄤ腑鏈変竴涓幆锛屽叾灏鹃儴杩炴帴鍒扮浜屼釜鑺傜偣銆?br>
绀轰緥 2锛?/p>
杈撳叆锛歨ead = [1,2], pos = 0
杈撳嚭锛?span class="mq-429">true
瑙i噴锛氶摼琛ㄤ腑鏈変竴涓幆锛屽叾灏鹃儴杩炴帴鍒扮涓€涓妭鐐广€?br>
绀轰緥 3锛?/p>
杈撳叆锛歨ead = [1], pos = -1
杈撳嚭锛?span class="mq-437">false
瑙i噴锛氶摼琛ㄤ腑娌℃湁鐜€?br>
鎬濊矾鍒嗘瀽
閾捐〃鎴愮幆
闂涔熸槸闈炲父缁忓吀鐨勭畻娉曢棶棰橈紝鍦ㄩ潰璇曚腑涔熺粡甯镐細閬囧埌銆?img data-ratio="0.4117647058823529" src="/img?url=https://mmbiz.qpic.cn/mmbiz_png/LNrWl4n5XIKtv5uplOwZ663ZIcCoriaYtAN2mcCLicPicDvooDAlsG3Q9IzS9vxHDkTKxIAL5Fy1mE3af65kvDaSA/640?wx_fmt=png" data-type="png" data-w="1360" alt="銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫? class="mq-446">
瑙e喅杩欑闂涓€鑸湁甯歌鐨勪袱绉嶆柟娉曪細 缁欐瘡涓凡閬嶅巻杩囩殑鑺傜偣鍔犳爣蹇椾綅锛岄亶鍘嗛摼琛紝褰撳嚭鐜颁笅涓€涓妭鐐瑰凡琚爣蹇楁椂锛屽垯璇佹槑鍗曢摼琛ㄦ湁鐜€?/p>
璁剧疆蹇參涓や釜鎸囬拡锛岄亶鍘嗗崟閾捐〃锛屽揩鎸囬拡涓€娆¤蛋涓ゆ锛屾參鎸囬拡涓€娆¤蛋涓€姝ワ紝濡傛灉鍗曢摼琛ㄤ腑瀛樺湪鐜紝鍒欏揩鎱㈡寚閽堢粓浼氭寚鍚戝悓涓€涓妭鐐癸紝鍚﹀垯鐩村埌蹇寚閽堟寚鍚?code class="mq-473">null鏍囧織娉?/code>鍜?code class="mq-449">蹇參鎸囬拡娉?/code>銆?/p>
鏃讹紝蹇參鎸囬拡閮戒笉鍙兘鐩搁亣銆?/p>
鏍囧織娉?span class="mq-452">
/**
* 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
};蹇參鎸囬拡锛堝弻鎸囬拡娉曪級
/**
* 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;
};
鏈夋晥鐨勬嫭鍙?馃崏
鉂?/span>棰樼洰闅惧害
easy
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鏍堛€佸搱甯岃〃銆?/p> 鉂?/span>
棰樼洰鎻忚堪
缁欏畾涓€涓彧鍖呮嫭'('
锛?code class="mq-506">')'锛?code class="mq-507">'{'锛?code class="mq-508">'}'锛?code class="mq-509">'['锛?code class="mq-510">']' 鐨勫瓧绗︿覆锛屽垽鏂瓧绗︿覆鏄惁鏈夋晥銆?/p>
鏈夋晥瀛楃涓查渶婊¤冻锛?/p>
1銆佸乏鎷彿蹇呴』鐢ㄧ浉鍚岀被鍨嬬殑鍙虫嫭鍙烽棴鍚堛€?2銆佸乏鎷彿蹇呴』浠ユ纭殑椤哄簭闂悎銆?/p>
娉ㄦ剰绌哄瓧绗︿覆鍙璁や负鏄湁鏁堝瓧绗︿覆銆?/p>
绀轰緥 1:
杈撳叆: "()"
杈撳嚭: true
绀轰緥 2:
杈撳叆: "()[]{}"
杈撳嚭: true
绀轰緥 3:
杈撳叆: "(]"
杈撳嚭: false
绀轰緥 4:
杈撳叆: "([)]"
杈撳嚭: false
绀轰緥 5:
杈撳叆: "{[]}"
杈撳嚭: true
鎬濊矾鍒嗘瀽
杩欓亾棰樺彲浠ュ埄鐢?code class="mq-544">鏍?/code>缁撴瀯銆?/p>
鎬濊矾澶ф鏄細閬囧埌宸︽嫭鍙凤紝涓€寰嬫帹鍏ユ爤涓紝閬囧埌鍙虫嫭鍙凤紝灏嗘爤椤堕儴鍏冪礌鎷垮嚭锛屽鏋滀笉鍖归厤鍒欒繑鍥?false
锛屽鏋滃尮閰嶅垯缁х画寰幆銆?img data-ratio="0.516256157635468" src="/img?url=https://mmbiz.qpic.cn/mmbiz_png/LNrWl4n5XIKtv5uplOwZ663ZIcCoriaYtKIbhQctkiclHdvagL2UCicJE2AhTIibHaOGAolwJW0R41GGIdMDD88mrg/640?wx_fmt=png" data-type="png" data-w="2030" alt="銆岄潰璇曞繀闂€峫eetcode楂橀棰樼簿閫? class="mq-547">绗竴绉嶈В娉曟槸鍒╃敤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 class="mq-603">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
};
婊戝姩绐楀彛鏈€澶у€?鉀?/span>
鉂?/span>棰樼洰闅惧害
hard
锛屾秹鍙婂埌鐨勭畻娉曠煡璇嗘湁鍙岀闃熷垪銆?/p> 鉂?/span>
棰樼洰鎻忚堪
缁欏畾涓€涓暟缁?nums锛屾湁涓€涓ぇ灏忎负 k 鐨勬粦鍔ㄧ獥鍙d粠鏁扮粍鐨勬渶宸︿晶绉诲姩鍒版暟缁勭殑鏈€鍙充晶銆備綘鍙彲浠ョ湅鍒板湪婊戝姩绐楀彛鍐呯殑 k 涓暟瀛椼€傛粦鍔ㄧ獥鍙f瘡娆″彧鍚戝彸绉诲姩涓€浣嶃€?/p>
杩斿洖婊戝姩绐楀彛涓殑鏈€澶у€笺€?/p>
杩涢樁锛氫綘鑳藉湪绾挎€ф椂闂村鏉傚害鍐呰В鍐虫棰樺悧锛?/p>
绀轰緥:
杈撳叆: nums = [1,3,-1,-3,5,3,6,7], 鍜?nbsp;k = 3
杈撳嚭: [3,3,5,5,6,7]
瑙i噴:
婊戝姩绐楀彛鐨勪綅缃?nbsp; 鏈€澶у€?br>--------------- -----
[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>
-
閬嶅巻鏁扮粍 -
渚濇閬嶅巻姣忎釜鍖洪棿鍐呯殑鏈€澶у€硷紝鏀惧叆鏁扮粍涓?
/**
* @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>
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章