Posted 鍏ㄦ爤淇粰涔嬭矾
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了相关的知识,希望对你有一定的参考价值。
鍓嶈█
鏁版嵁缁撴瀯涓殑閾捐〃杩樻槸寰堥噸瑕佺殑锛屾墍浠ヨ繖绔犺妭鎶婂墤鎸噊ffer 鍜?LeetCode 涓殑鐩稿叧棰樼洰鍋氫竴涓眹鎬伙紝鍒嗕韩缁欏ぇ瀹娥煠€?/p>
閾捐〃 Linked List
涓€绉嶅父瑙佺殑鍩虹鏁版嵁缁撴瀯锛屼篃鏄竴绉嶇嚎鎬ц〃锛屼絾鏄苟涓嶄細鎸夌嚎鎬ц〃鐨勯『搴忓瓨鍌ㄦ暟鎹紝鑰屾槸鍦ㄦ瘡涓€涓妭鐐归噷瀛樺埌涓嬩竴涓妭鐐圭殑鎸囬拡(Pointer)銆?/p>
閾捐〃鍦ㄦ彃鍏ョ殑鏃跺€欏彲浠ヨ揪鍒?O(1) 鐨勫鏉傚害锛屾瘮鍙︿竴绉嶇嚎鎬ц〃 鈥斺€?椤哄簭琛ㄥ揩寰楀锛屼絾鏄煡鎵句竴涓妭鐐规垨鑰呰闂壒瀹氱紪鍙风殑鑺傜偣鍒欓渶瑕?O(n)鐨勬椂闂达紝鑰岄『搴忚〃鐩稿簲鐨勬椂闂村鏉傚害鍒嗗埆鏄?O(log n) 鍜?O(1)銆?/p>
浼樼己鐐癸細
浣跨敤閾捐〃缁撴瀯鍙互鍏嬫湇鏁扮粍閾捐〃闇€瑕侀鍏堢煡閬撴暟鎹ぇ灏忕殑缂虹偣锛岄摼琛ㄧ粨鏋勫彲浠ュ厖鍒嗗埄鐢ㄨ绠楁満鍐呭瓨绌洪棿锛屽疄鐜扮伒娲荤殑鍐呭瓨鍔ㄦ€佺鐞嗐€備絾鏄摼琛ㄥけ鍘讳簡鏁扮粍闅忔満璇诲彇鐨勪紭鐐癸紝鍚屾椂閾捐〃鐢变簬澧炲姞浜嗙粨鐐圭殑鎸囬拡鍩燂紝绌洪棿寮€閿€姣旇緝澶с€?/p>
閾捐〃鍏佽鎻掑叆鍜岀Щ闄よ〃涓婁换鎰忎綅缃笂鐨勮妭鐐癸紝浣嗘槸涓嶅厑璁搁殢鏈哄瓨鍙栥€?/strong>
閾捐〃鏈夊緢澶氱涓嶅悓鐨勭被鍨嬶細
-
鍗曞悜閾捐〃 -
鍙屽悜閾捐〃 -
寰幆閾捐〃
閾捐〃閫氬父鍙互琛嶇敓鍑哄惊鐜摼琛紝闈欐€侀摼琛紝鍙岄摼琛ㄧ瓑銆傚浜庨摼琛ㄤ娇鐢紝闇€瑕佹敞鎰?span>澶寸粨鐐?/strong>鐨勪娇鐢ㄣ€?/p>
閾捐〃鍒涘缓
//鍗曢摼琛ㄦ彃鍏ャ€佸垹闄ゃ€佹煡鎵?/span>
class LinkedList {
constructor(val) {
val = val === undefined ? 'head' : val;
this.head = new ListNode(val)
}
// 鎵緑al鍊艰妭鐐癸紝娌℃湁鎵惧埌杩斿洖-1
findByVal(val) {
let current = this.head
while (current !== null && current.val !== val) {
current = current.next
}
return current ? current : -1
}
// 鎻掑叆鑺傜偣,鍦ㄥ€间负val鍚庨潰鎻掑叆
insert(newVal, val) {
let current = this.findByVal(val)
if (current === -1) return false
let newNode = new ListNode(newVal)
newNode.next = current.next
current.next = newNode
}
// 鑾峰彇鍊间负nodeVal鐨勫墠涓€涓妭鐐?鎵句笉鍒颁负-1,鍙傛暟鏄痸al
// 閫傜敤浜庨摼琛ㄤ腑鏃犻噸澶嶈妭鐐?/span>
findNodePreByVal(nodeVal) {
let current = this.head;
while (current.next !== null && current.next.val !== nodeVal)
current = current.next
return current !== null ? current : -1
}
// 鏍规嵁index鏌ユ壘褰撳墠鑺傜偣, 鍙傛暟涓篿ndex
// 鍙互浣滀负姣旇緝閾捐〃鏄惁鏈夐噸澶嶈妭鐐?/span>
findByIndex(index) {
let current = this.head,
pos = 1
while (current.next !== null && pos !== index) {
current = current.next
pos++
}
return (current && pos === index) ? current : -1
}
// 鍒犻櫎鏌愪竴涓妭鐐?鍒犻櫎澶辫触鏀惧洖false
remove(nodeVal) {
if(nodeVal === 'head') return false
let needRemoveNode = this.findByVal(nodeVal)
if (needRemoveNode === -1) return false
let preveNode = this.findNodePreByVal(nodeVal)
preveNode.next = needRemoveNode.next
}
//閬嶅巻鑺傜偣
disPlay() {
let res = new Array()
let current = this.head
while (current !== null) {
res.push(current.val)
current = current.next
}
return res
}
// 鍦ㄩ摼琛ㄦ湯灏炬彃鍏ヤ竴涓柊鐨勮妭鐐?/span>
push(nodeVal) {
let current = this.head
let node = new ListNode(nodeVal)
while (current.next !== null)
current = current.next
current.next = node
}
// 鍦ㄥご閮ㄦ彃鍏?/span>
frontPush(nodeVal) {
let newNode = new ListNode(nodeVal)
this.insert(nodeVal,'head')
}
}
褰撶劧浜嗭紝鍙兘杩樻湁涓€浜涘叾浠栫殑鏂规硶鎴戞槸娌℃湁鎯冲埌鐨勶紝鍓╀笅鐨勫彲浠ヨ嚜琛屽幓瀹屾垚
閾捐〃绫荤殑浣跨敤
let demo = new LinkedList() // LinkedList {head: ListNode}
// console.log((demo.disPlay()))
demo.push('1232')
demo.insert(123, 'head');
demo.push('last value')
demo.frontPush('start')
demo.remove('head')
// demo.remove('last value')
// console.log(demo.remove('head'))
// demo.push('2132')
// demo.insert('涓嶅瓨鍦ㄧ殑鍊?, '鎻掑叆澶辫触') //return -1
console.log(demo.findByIndex(1))
console.log((demo.disPlay()))
涓婇潰鐨勪唬鐮佺墖娈垫槸娴嬭瘯鐢ㄥ埌锛屾祴璇曡繃浜嗭紝鍩烘湰涓婃病鏈変笂闈㈠ぇ闂锛屽綋鐒朵簡锛屾湁浜涚粏鏋濇湯鑺傜殑鍦版柟杩樻槸寰楁敞鎰忕殑锛屾瘮濡?code class="mq-122">findByIndex杩欎釜鍑芥暟涓?code class="mq-123">pos = 0 杩樻槸 pos = 1
闂锛屽彇鍐充簬鑷繁锛岃繕鏈夌殑璇濓紝remove
鍑芥暟鍒板簳鑳戒笉鑳藉垹闄?head'澶磋妭鐐癸紝杩欓兘鏄病鏈夊噯纭殑鏍囧噯鐨勶紝杩欎釜鍙互鏍规嵁鑷繁鎯呭喌鑰屽畾锛?/p>
涓€瀹氳浣忥紝涓嶆槸鍞竴鏍囧噯锛屼綘璁や负鍙互鍒犻櫎'head'鐨勮瘽锛屼篃娌℃湁闂銆?/p>
鍙屽悜閾捐〃
鍙岄摼琛ㄤ互绫讳技鐨勬柟寮忓伐浣滐紝浣?code class="mq-129">杩樻湁涓€涓紩鐢ㄥ瓧娈?/code>锛岀О涓?code class="mq-130">鈥減rev鈥?/code>瀛楁銆傛湁浜嗚繖涓澶栫殑瀛楁锛屾偍灏辫兘澶熺煡閬撳綋鍓嶇粨鐐圭殑鍓嶄竴涓粨鐐广€?/p>
璁╂垜浠湅涓€涓緥瀛愶細
缁胯壊绠ご琛ㄧず鎴戜滑鐨勨€減rev鈥濆瓧娈垫槸濡備綍宸ヤ綔鐨勩€?/p>
缁撴瀯绫讳技馃憞
class doubleLinkNode {
constructor (val) {
this.val = val
this.prev = null
this.next = null
}
}
涓庡崟閾炬帴鍒楄〃绫讳技锛屾垜浠皢浣跨敤 瀵逛簬鎻掑叆鍜屽垹闄わ紝鐩告瘮杈冨崟閾捐〃鑰岃█锛屼細绋嶅井澶嶆潅涓€浜涳紝鍥犱负鎴戜滑杩橀渶瑕佸鐞嗏€減rev鈥濆瓧娈点€?/p>
娣诲姞鎿嶄綔-鍙岄摼琛?/strong> 涓句釜渚嬪瓙鍚э紝褰撶劧浜嗭紝鏈€濂界殑褰㈠紡灏辨槸鐢诲浘鏉ヨВ鍐炽€?/p>
澶寸粨鐐?/code>鏉ヨ〃绀烘暣涓垪琛ㄣ€?/p>
鍏抽敭鐐瑰垎鏋?/strong>
-
寤虹珛涓€涓猲ewNode -
瀵归摼琛ㄨ繘琛宬涓崟浣嶅垎缁勶紝璁板綍姣忎竴缁勭殑璧峰鍜屾渶鍚庤妭鐐逛綅缃? -
瀵规瘡涓€缁勮繘琛岀浉搴旂殑缈昏浆锛岃寰楁洿鎹綅缃? -
杩斿洖newNode.next
var reverseKGroup = (head, k) => {
let reverseList = (start, end) => {
let [pre, cur] = [start, start.next],
front = cur;
// 缁堟鏉′欢灏辨槸cur褰撳墠鑺傜偣涓嶈兘绛変簬end鑺傜偣
// 缈昏浆鐨勫璺?/span>
while( cur !== end) {
let next = cur.next
cur.next = pre
pre = cur
cur = next
}
front.next = end // 鏂扮炕杞摼琛ㄩ渶瑕佽繛鎺?涔熷氨鏄痜ront鎸囧悜鍘熸潵鍖洪棿鍚庝竴涓妭鐐?/span>
start.next = pre // 鏂扮炕杞殑寮€澶撮渶瑕佽繛鎺tart.next
return front // 杩斿洖缈昏浆鍚庨渶瑕佽繛鎺ラ摼琛?涔熷氨鏄痜ront鎸囧悜
}
let newNode = new ListNode('start')
newNode.next = head;
let [start, end] = [newNode,newNode.next],
count = 0;
while(end !== null ) {
count++
if( count % k === 0) {
// k涓妭鐐圭炕杞悗,鍙堥噸鏂板紑濮?杩斿洖鍊煎氨鏄痚nd鑺傜偣鍓嶉潰涓€涓?/span>
start = reverseList(start, end.next)
end = start.next
}else{
//涓嶆槸涓€涓垎缁勫氨鎸囧悜涓嬩竴涓妭鐐?/span>
end = end.next
}
}
return newNode.next
};
濂藉浼欙紝闈㈣瘯鐨勬椂鍊欙紝瑕佹垜鍐欒繖涓紝涓嶈鎴戠敾鍥剧殑璇濓紝鎴戞娊璞′笉鍑烘潵馃挗馃挗
[浠g爜鐐硅繖閲岎煠璢(https://github.com/daydaylee1227/Blog/blob/master/绠楁硶/閾捐〃/leetcode-K 涓竴缁勭炕杞摼琛?js)
鍚堝苟K涓帓搴忛摼琛ㄢ瓙猸愨瓙
棰樼洰鎻忚堪锛氬悎骞?k 涓帓搴忛摼琛紝杩斿洖鍚堝苟鍚庣殑鎺掑簭閾捐〃銆傝鍒嗘瀽鍜屾弿杩扮畻娉曠殑澶嶆潅搴︺€?/p>
閾炬帴锛歔鍚堝苟K涓帓搴忛摼琛╙(https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
绀轰緥:
杈撳叆:
[
1->4->5,
1->3->4,
2->6
]
杈撳嚭: 1->1->2->3->4->4->5->6
[鍥炴枃閾捐〃]猸?span class="mq-480">
棰樼洰鎻忚堪锛氳鍒ゆ柇涓€涓摼琛ㄦ槸鍚︿负鍥炴枃閾捐〃銆?/p>
閾炬帴锛歭eetcode-鍥炴枃閾捐〃
绀轰緥:
杈撳叆:
[
1->4->5,
1->3->4,
2->6
]
杈撳嚭: 1->1->2->3->4->4->5->6
绀轰緥 1:
杈撳叆: 1->2
杈撳嚭: false
绀轰緥 2:
杈撳叆: 1->2->2->1
杈撳嚭: true
瑙i鎬濊矾锛?/p>
鎵惧埌閾捐〃涓偣锛岀劧鍚庡皢鍚庡崐閮ㄥ垎鍙嶈浆锛屽氨鍙互渚濇姣旇緝寰楀嚭缁撹浜嗐€?/p>
鍏抽敭灏辨槸鎬庝箞鍘绘壘涓偣鍛紵
蹇參鎸囬拡
杩欎釜鍦ㄩ摼琛ㄤ腑搴旂敤澶箍娉涗簡锛屾€濊矾灏辨槸:璁剧疆涓€涓腑闂存寚閽?mid锛屽湪涓€娆¢亶鍘嗕腑锛宧ead 璧颁袱鏍硷紝mid 璧颁竴鏍硷紝褰?head 鍙栧埌鏈€鍚庝竴涓€兼垨鑰呰烦鍑烘椂锛宮id 灏辨寚鍚戜腑闂寸殑鍊笺€?/p>
let mid = head
// 寰幆鏉′欢锛氬彧瑕乭ead瀛樺湪鍒欐渶灏戣蛋涓€娆?br>while(head !== null && head.next !== null) {
head = head.next.next // 鎸囬拡涓€娆¤蛋涓ゆ牸
mid = mid.next// 涓棿鎸囬拡涓€娆¤蛋涓€鏍?br>}
閬嶅巻鐨勬椂鍊欓€氳繃杩唬鏉ュ弽杞摼琛紝mid 涔嬪墠鐨?node 閮戒細琚弽杞€備娇鐢ㄨ凯浠f潵鍙嶈浆銆?/p>
while(head !== null && head.next !== null) {
pre = mid
mid = mid.next
head = head.next.next
pre.next = reversed
reversed = pre
}
渚嬪锛?/p>
濂囨暟锛? -> 2 -> 3 -> 2 ->1
閬嶅巻瀹屾垚鍚庯細mid = 3->2->1
reversed = 2->1
鍋舵暟锛? -> 2 -> 2 ->1
閬嶅巻瀹屾垚鍚庯細mid = 2->1
reversed = 2->1
瀹屾暣浠g爜:
var isPalindrome = function (head) {
if (head === null || head.next === null) return true;
let mid = head,
pre = null,
reversed = null; // reversed缈昏浆鐨勯摼琛?/span>
while (head !== null && head.next !== null) {
// 甯歌缈昏浆鐨勫璺?/span>
pre = mid
mid = mid.next
head = head.next.next
pre.next = reversed
reversed = pre
}
// 鍒ゆ柇閾捐〃鏁版槸涓嶆槸濂囨暟,鏄殑璇漨id寰€鍚庤蛋涓€浣?/span>
if (head) mid = mid.next
while (mid) {
if (reversed.val !== mid.val) return false
reversed = reversed.next
mid = mid.next
}
return true
};
[閾捐〃鐩镐氦]猸?span class="mq-545">
棰樼洰鎻忚堪锛氱粰瀹氫袱涓紙鍗曞悜锛夐摼琛紝鍒ゅ畾瀹冧滑鏄惁鐩镐氦骞惰繑鍥炰氦鐐广€傝娉ㄦ剰鐩镐氦鐨勫畾涔夊熀浜庤妭鐐圭殑寮曠敤锛岃€屼笉鏄熀浜庤妭鐐圭殑鍊笺€傛崲鍙ヨ瘽璇达紝濡傛灉涓€涓摼琛ㄧ殑绗琸涓妭鐐逛笌鍙︿竴涓摼琛ㄧ殑绗琷涓妭鐐规槸鍚屼竴鑺傜偣锛堝紩鐢ㄥ畬鍏ㄧ浉鍚岋級锛屽垯杩欎袱涓摼琛ㄧ浉浜ゃ€?/p>
閾炬帴锛歔leetcode-閾捐〃鐩镐氦]
绀轰緥 1锛?/p>
杈撳叆锛歩ntersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
杈撳嚭锛歊eference of the node with value = 8
杈撳叆瑙i噴锛氱浉浜よ妭鐐圭殑鍊间负 8 锛堟敞鎰忥紝濡傛灉涓や釜鍒楄〃鐩镐氦鍒欎笉鑳戒负 0锛夈€備粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[4,1,8,4,5]锛岄摼琛?B 涓?nbsp;[5,0,1,8,4,5]銆傚湪 A 涓紝鐩镐氦鑺傜偣鍓嶆湁 2 涓妭鐐癸紱鍦?B 涓紝鐩镐氦鑺傜偣鍓嶆湁 3 涓妭鐐广€?br>
绀轰緥 2锛?/p>
杈撳叆锛歩ntersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
杈撳嚭锛歊eference of the node with value = 2
杈撳叆瑙i噴锛氱浉浜よ妭鐐圭殑鍊间负 2 锛堟敞鎰忥紝濡傛灉涓や釜鍒楄〃鐩镐氦鍒欎笉鑳戒负 0锛夈€備粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[0,9,1,2,4]锛岄摼琛?B 涓?nbsp;[3,2,4]銆傚湪 A 涓紝鐩镐氦鑺傜偣鍓嶆湁 3 涓妭鐐癸紱鍦?B 涓紝鐩镐氦鑺傜偣鍓嶆湁 1 涓妭鐐广€?br>
绀轰緥 3锛?/p>
杈撳叆锛歩ntersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
杈撳嚭锛歯ull
杈撳叆瑙i噴锛氫粠鍚勮嚜鐨勮〃澶村紑濮嬬畻璧凤紝閾捐〃 A 涓?nbsp;[2,6,4]锛岄摼琛?B 涓?nbsp;[1,5]銆傜敱浜庤繖涓や釜閾捐〃涓嶇浉浜わ紝鎵€浠?intersectVal 蹇呴』涓?nbsp;0锛岃€?skipA 鍜?skipB 鍙互鏄换鎰忓€笺€?br>瑙i噴锛氳繖涓や釜閾捐〃涓嶇浉浜わ紝鍥犳杩斿洖 null銆?br>
鎬濊矾锛?/p>
-
璁剧疆涓や釜鎸囬拡,姣忔潯鎸囬拡璧板畬鑷繁鐨勮矾鍚?鎸囧悜鍙﹀涓€涓摼琛?閭d箞涓や釜鑺傜偣鐩哥瓑鐨勮瘽锛屼竴瀹氭槸鍚屼竴涓偣銆? -
鍥犱负涓や釜鎸囬拡璧扮殑璺濈鏄竴鏍风殑,鑰屼笖姣忔閮藉墠杩?锛岃窛绂荤浉绛?閫熷害鐩稿悓,濡傛灉鐩哥瓑锛屼竴瀹氭槸鍚屼竴涓偣銆?
var getIntersectionNode = function (headA, headB) {
let p1 = headA,
p2 = headB;
while (p1 != p2) {
p1 = p1 === null ? headB : p1.next
p2 = p2 === null ? headA : p2.next
}
return p1
};
鉂わ笍 鎰熻阿澶у
以上是关于的主要内容,如果未能解决你的问题,请参考以下文章