close

緣起

現在大廠面試中,算法題幾乎為必考項,且近幾年頻現 LeetCode 真題,此篇為拿到字節、騰訊、京東 Offer 的筆者本人在準備面試過程中親自刷過以及遇到過高頻算法題。文章內容會分模塊整理,對於筆者在面試過程中遇到的真題,會給予着重 【🔥】標出。

同時,可以毫不客氣的說,如果你準備時間有限,又想追求算法題準備效率最大化,那麼你只需要按照大綱把下面的題目刷完,並把代碼爛熟於心,就幾乎可以應對 90% 的面試算法考題了。

整理這篇內容的目的一個是筆者在之前準備面試時的一點積累,而它確實也幫助筆者在面試算法題中過關斬將,同時呢,也希望能夠在金三銀四給予拼搏的你,一點點幫助就好!💪

文末有福利 :)😈

本篇內容包括如下模塊:

高頻算法題系列:鍊表
【🔥】【有真題】高頻算法題系列:字符串
【🔥】【有真題】高頻算法題系列:數組問題
高頻算法題系列:二叉樹
【🔥】高頻算法題系列:排序算法
【🔥】高頻算法題系列:二分查找
【🔥】高頻算法題系列:動態規劃
高頻算法題系列:BFS
【🔥】高頻算法題系列:棧
【🔥】高頻算法題系列:DFS
【🔥】高頻算法題系列:回溯算法

其中標🔥的部分代表非常高頻的考題,其中不乏筆者遇到的原題。其中對於每一類,首先會列出包含的考題,然後針對每一道考題會給出難度、考察知識點、是否是面試真題,在每道題詳細介紹時,還會給出每道題的 LeetCode 鏈接,幫助讀者理解題意,以及能夠進行實際的測驗,還可以觀看其他人的答案,更好的幫助準備。

高頻算法題系列:鍊表

筆者遇到的高頻鍊表題主要包含這幾道:

通過快慢指針尋找鍊表中點 【簡單】
通過鍊表的後續遍歷判斷回文鍊表問題 【簡單】
鍊表的反向輸出 【簡單】
合併 K 個升序鍊表 【困難】
K個一組翻轉鍊表 【困難】
環形鍊表 【簡單】
排序鍊表 【中等】
相交鍊表 【簡單】
尋找鍊表中點題解

通過快慢指針尋找鍊表中點

/****/functionfindCenter(head){letslower=head,faster=head;while(faster&&faster.next!=null){slower=slower.next;faster=faster.next.next;}//如果faster不等於null,說明是奇數個,slower再移動一格if(faster!=null){slower=slower.next;}returnslower;}前序遍歷判斷回文鍊表

👉 【LeetCode 直通車】:234 回文鍊表(簡單)[1]

題解1

利用鍊表的後續遍歷,使用函數調用棧作為後序遍歷棧,來判斷是否回文

/****/varisPalindrome=function(head){letleft=head;functiontraverse(right){if(right==null)returntrue;letres=traverse(right.next);res=res&&(right.val===left.val);left=left.next;returnres;}returntraverse(head);};題解2

通過 快、慢指針找鍊表中點,然後反轉鍊表,比較兩個鍊表兩側是否相等,來判斷是否是回文鍊表

/****/varisPalindrome=function(head){//反轉slower鍊表letright=reverse(findCenter(head));letleft=head;//開始比較while(right!=null){if(left.val!==right.val){returnfalse;}left=left.next;right=right.next;}returntrue;}functionfindCenter(head){letslower=head,faster=head;while(faster&&faster.next!=null){slower=slower.next;faster=faster.next.next;}//如果faster不等於null,說明是奇數個,slower再移動一格if(faster!=null){slower=slower.next;}returnslower;}functionreverse(head){letprev=null,cur=head,nxt=head;while(cur!=null){nxt=cur.next;cur.next=prev;prev=cur;cur=nxt;}returnprev;}反轉鍊表

👉 【LeetCode 直通車】:206 反轉鍊表(簡單)[2]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@return{ListNode}*/varreverseList=function(head){if(head==null||head.next==null)returnhead;letlast=reverseList(head.next);head.next.next=head;head.next=null;returnlast;};合併K個升序鍊表

👉 【LeetCode 直通車】:23 合併K個升序鍊表(困難)[3]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode[]}lists*@return{ListNode}*/varmergeKLists=function(lists){if(lists.length===0)returnnull;returnmergeArr(lists);};functionmergeArr(lists){if(lists.length<=1)returnlists[0];letindex=Math.floor(lists.length/2);constleft=mergeArr(lists.slice(0,index))constright=mergeArr(lists.slice(index));returnmerge(left,right);}functionmerge(l1,l2){if(l1==null&&l2==null)returnnull;if(l1!=null&&l2==null)returnl1;if(l1==null&&l2!=null)returnl2;letnewHead=null,head=null;while(l1!=null&&l2!=null){if(l1.val<l2.val){if(!head){newHead=l1;head=l1;}else{newHead.next=l1;newHead=newHead.next;}l1=l1.next;}else{if(!head){newHead=l2;head=l2;}else{newHead.next=l2;newHead=newHead.next;}l2=l2.next;}}newHead.next=l1?l1:l2;returnhead;}K 個一組翻轉鍊表

👉 【LeetCode 直通車】:25 K 個一組翻轉鍊表(困難)[4]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@param{number}k*@return{ListNode}*/varreverseKGroup=function(head,k){leta=head,b=head;for(leti=0;i<k;i++){if(b==null)returnhead;b=b.next;}constnewHead=reverse(a,b);a.next=reverseKGroup(b,k);returnnewHead;};functionreverse(a,b){letprev=null,cur=a,nxt=a;while(cur!=b){nxt=cur.next;cur.next=prev;prev=cur;cur=nxt;}returnprev;}環形鍊表

👉 【LeetCode 直通車】:141 環形鍊表(簡單)[5]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@return{boolean}*/varhasCycle=function(head){if(head==null||head.next==null)returnfalse;letslower=head,faster=head;while(faster!=null&&faster.next!=null){slower=slower.next;faster=faster.next.next;if(slower===faster)returntrue;}returnfalse;};排序鍊表

👉 【LeetCode 直通車】:148 排序鍊表(中等)[6]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}head*@return{ListNode}*/varsortList=function(head){if(head==null)returnnull;letnewHead=head;returnmergeSort(head);};functionmergeSort(head){if(head.next!=null){letslower=getCenter(head);letnxt=slower.next;slower.next=null;console.log(head,slower,nxt);constleft=mergeSort(head);constright=mergeSort(nxt);head=merge(left,right);}returnhead;}functionmerge(left,right){letnewHead=null,head=null;while(left!=null&&right!=null){if(left.val<right.val){if(!head){newHead=left;head=left;}else{newHead.next=left;newHead=newHead.next;}left=left.next;}else{if(!head){newHead=right;head=right;}else{newHead.next=right;newHead=newHead.next;}right=right.next;}}newHead.next=left?left:right;returnhead;}functiongetCenter(head){letslower=head,faster=head.next;while(faster!=null&&faster.next!=null){slower=slower.next;faster=faster.next.next;}returnslower;}相交鍊表

👉 【LeetCode 直通車】:160 相交鍊表(簡單)[7]

題解/***Definitionforsingly-linkedlist.*functionListNode(val){*this.val=val;*this.next=null;*}*//***@param{ListNode}headA*@param{ListNode}headB*@return{ListNode}*/vargetIntersectionNode=function(headA,headB){letlastHeadA=null;letlastHeadB=null;letoriginHeadA=headA;letoriginHeadB=headB;if(!headA||!headB){returnnull;}while(true){if(headB==headA){returnheadB;}if(headA&&headA.next==null){lastHeadA=headA;headA=originHeadB;}else{headA=headA.next;}if(headB&&headB.next==null){lastHeadB=headBheadB=originHeadA;}else{headB=headB.next;}if(lastHeadA&&lastHeadB&&lastHeadA!=lastHeadB){returnnull;}}returnnull;};【🔥】高頻算法題系列:字符串

主要有以下幾類高頻考題:

最長回文子串 【中等】【雙指針】【面試真題】
最長公共前綴 【簡單】【雙指針】
無重複字符的最長子串【中等】【雙指針】
最小覆蓋子串 【困難】【滑動窗口】【面試真題】
【面試真題】最長回文子串【雙指針】

👉 【LeetCode 直通車】:5 最長回文子串(中等)[8]

題解/***@param{string}s*@return{string}*/varlongestPalindrome=function(s){if(s.length===1)returns;letmaxRes=0,maxStr='';for(leti=0;i<s.length;i++){letstr1=palindrome(s,i,i);letstr2=palindrome(s,i,i+1);if(str1.length>maxRes){maxStr=str1;maxRes=str1.length;}if(str2.length>maxRes){maxStr=str2;maxRes=str2.length;}}returnmaxStr;};functionpalindrome(s,l,r){while(l>=0&&r<s.length&&s[l]===s[r]){l--;r++;}returns.slice(l+1,r);}最長公共前綴【雙指針】

👉 【LeetCode 直通車】:14 最長公共前綴(簡單)[9]

題解/***@param{string[]}strs*@return{string}*/varlongestCommonPrefix=function(strs){if(strs.length===0)return"";letfirst=strs[0];if(first==="")return"";letminLen=Number.MAX_SAFE_INTEGER;for(leti=1;i<strs.length;i++){constlen=twoStrLongestCommonPrefix(first,strs[i]);minLen=Math.min(len,minLen);}returnfirst.slice(0,minLen);};functiontwoStrLongestCommonPrefix(s,t){leti=0,j=0;letcnt=0;while(i<s.length&&j<t.length){console.log(s[i],t[j],cnt)if(s[i]===t[j]){cnt++;}else{returncnt;}i++;j++;}returncnt;}無重複字符的最長子串【雙指針】

👉 【LeetCode 直通車】:3 無重複字符的最長子串(中等)[10]

題解/***@param{string}s*@return{number}*/varlengthOfLongestSubstring=function(s){letwindow={};letleft=0,right=0;letmaxLen=0,maxStr='';while(right<s.length){letc=s[right];right++;if(window[c])window[c]++;elsewindow[c]=1while(window[c]>1){letd=s[left];left++;window[d]--;}if(maxLen<right-left){maxLen=right-left;}}returnmaxLen;};【面試真題】 最小覆蓋子串【滑動窗口】

👉 【LeetCode 直通車】:76 最小覆蓋子串(困難)[11]

題解/***@param{string}s*@param{string}t*@return{string}*/varminWindow=function(s,t){letneed={},window={};for(letcoft){if(!need[c])need[c]=1;elseneed[c]++;}letleft=0,right=0;letvalid=0,len=Object.keys(need).length;letminLen=s.length+1,minStr='';while(right<s.length){constd=s[right];right++;if(!window[d])window[d]=1;elsewindow[d]++;if(need[d]&&need[d]===window[d]){valid++;}console.log('left-right',left,right);while(valid===len){if(right-left<minLen){minLen=right-left;minStr=s.slice(left,right);}console.loletc=s[left];left++;window[c]--;if(need[c]&&window[c]<need[c]){valid--;}}}returnminStr;};【🔥】高頻算法題系列:數組問題

主要有幾類高頻考題:

俄羅斯套娃信封問題【困難】【排序+最長上升子序列】【面試真題】
最長連續遞增序列 【簡單】【雙指針】
最長連續序列【困難】【哈希表】
盛最多水的容器【困難】【面試真題】
尋找兩個正序數組的中位數【困難】【雙指針】
刪除有序數組中的重複項【簡單】【快慢指針】
和為K的子數組【中等】【哈希表】
nSum 問題【系列】【簡單】【哈希表】
接雨水【困難】【暴力+備忘錄優化】【面試真題】
跳躍遊戲【系列】【中等】【貪心算法】
【面試真題】俄羅斯套娃信封問題【排序+最長上升子序列】

👉 【LeetCode 直通車】:354 俄羅斯套娃信封問題(困難)[12]

題解/***@param{number[][]}envelopes*@return{number}*/varmaxEnvelopes=function(envelopes){if(envelopes.length===1)return1;envelopes.sort((a,b)=>{if(a[0]!==b[0])returna[0]-b[0];elsereturnb[1]-a[1];});letLISArr=[];for(let[key,value]ofenvelopes){LISArr.push(value);}console.log(LISArr);returnLIS(LISArr);};functionLIS(arr){letdp=[];letmaxAns=0;for(leti=0;i<arr.length;i++){dp[i]=1;}for(leti=1;i<arr.length;i++){for(letj=i;j>=0;j--){if(arr[i]>arr[j]){dp[i]=Math.max(dp[i],dp[j]+1)}maxAns=Math.max(maxAns,dp[i]);}}returnmaxAns;}最長連續遞增序列【快慢指針】

👉 【LeetCode 直通車】:674 最長連續遞增序列(簡單)[13]

題解/***@param{number[]}nums*@return{number}*/varfindLengthOfLCIS=function(nums){if(nums.length===0)return0;constn=nums.length;letleft=0,right=1;letglobalMaxLen=1,maxLen=1;while(right<n){if(nums[right]>nums[left])maxLen++;else{maxLen=1;}left++;right++;globalMaxLen=Math.max(globalMaxLen,maxLen);}returnglobalMaxLen;};最長連續序列 【哈希表】

👉 【LeetCode 直通車】:128 最長連續序列(困難)[14]

題解/***@param{number[]}nums*@return{number}*/varlongestConsecutive=function(nums){if(nums.length===0)return0;constset=newSet(nums);constn=nums.length;letglobalLongest=1;for(leti=0;i<n;i++){if(!set.has(nums[i]-1)){letlongest=1;letcurrentNum=nums[i];while(set.has(currentNum+1)){currentNum+=1;longest++;}globalLongest=Math.max(globalLongest,longest);}}returnglobalLongest;};【面試真題】盛最多水的容器【哈希表】

👉 【LeetCode 直通車】:11 盛最多水的容器(中等)[15]

題解/***@param{number[]}height*@return{number}*/varmaxArea=function(height){letn=height.length;letleft=0,right=n-1;letmaxOpacity=0;while(left<right){letres=Math.min(height[left],height[right])*(right-left);maxOpacity=Math.max(maxOpacity,res);if(height[left]<height[right])left++elseright--;}returnmaxOpacity;};尋找兩個正序數組的中位數【雙指針】

👉 【LeetCode 直通車】:4 尋找兩個正序數組的中位數(困難)[16]

題解/***@param{number[]}nums1*@param{number[]}nums2*@return{number}*/varfindMedianSortedArrays=function(nums1,nums2){letm=nums1.length,n=nums2.length;leti=0,j=0;letnewArr=[];while(i<m&&j<n){if(nums1[i]<nums2[j]){newArr.push(nums1[i++]);}else{newArr.push(nums2[j++]);}}newArr=newArr.concat(i<m?nums1.slice(i):nums2.slice(j));constlen=newArr.length;console.log(newArr)if(len%2===0){return(newArr[len/2]+newArr[len/2-1])/2;}else{returnnewArr[Math.floor(len/2)];}};刪除有序數組中的重複項【快慢指針】

👉 【LeetCode 直通車】:26 刪除有序數組中的重複項(簡單)[17]

題解/***@param{number[]}nums*@return{number}*/varremoveDuplicates=function(nums){if(nums.length<=1)returnnums.length;letlo=0,hi=0;while(hi<nums.length){while(nums[lo]===nums[hi]&&hi<nums.length)hi++;if(nums[lo]!==nums[hi]&&hi<nums.length){lo++;nums[lo]=nums[hi];}hi++;}returnlo+1;};

👉 【LeetCode 直通車】:695 島嶼的最大面積(中等)[18]

題解/***@param{number[][]}grid*@return{number}*/letmaxX,maxY;letvisited;letglobalMaxArea;varmaxAreaOfIsland=function(grid){visited=newSet();maxX=grid.length;maxY=grid[0].length;globalMaxArea=0;for(leti=0;i<maxX;i++){for(letj=0;j<maxY;j++){if(grid[i][j]===1){visited.add(`(${i},${j})`);globalMaxArea=Math.max(globalMaxArea,dfs(grid,i,j));}visited.clear();}}returnglobalMaxArea;};functiondfs(grid,x,y){letres=1;for(leti=-1;i<=1;i++){for(letj=-1;j<=1;j++){if(Math.abs(i)===Math.abs(j))continue;constnewX=x+i;constnewY=y+j;if(newX>=maxX||newX<0||newY>=maxY||newY<0)continue;if(visited.has(`(${newX},${newY})`))continue;visited.add(`(${newX},${newY})`);constareaCnt=grid[newX][newY]if(areaCnt===1){constcnt=dfs(grid,newX,newY);res+=cnt;}}}returnres;}和為K的子數組【哈希表】

👉 【LeetCode 直通車】:560 和為K的子數組(中等)[19]

題解/***@param{number[]}nums*@param{number}k*@return{number}*/varsubarraySum=function(nums,k){letcnt=0;letsum0_i=0,sum0_j=0;letmap=newMap();map.set(0,1);for(leti=0;i<=nums.length;i++){sum0_i+=nums[i];sum0_j=sum0_i-k;console.log('map',sum0_j,map.get(sum0_j))if(map.has(sum0_j)){cnt+=map.get(sum0_j);}letsumCnt=map.get(sum0_i)||0;map.set(sum0_i,sumCnt+1);}returncnt;};nSum問題【哈希表】【系列】
👉 【LeetCode 直通車】:1 兩數之和(簡單)[20]
👉 【LeetCode 直通車】:167 兩數之和 II - 輸入有序數組(簡單)[21]
👉 【LeetCode 直通車】:15 三數之和(中等)[22]
👉 【LeetCode 直通車】:18 四數之和(中等)[23]

受限於篇幅,這裡只給出第一道題的代碼模板,也是一面常考真題。

題解/***@param{number[]}nums*@param{number}target*@return{number[]}*/vartwoSum=function(nums,target){letmap2=newMap();for(leti=0;i<nums.length;i++){map2.set(nums[i],i);}for(leti=0;i<nums.length;i++){if(map2.has(target-nums[i])&&map2.get(target-nums[i])!==i)return[i,map2.get(target-nums[i])]}};接雨水【暴力+備忘錄優化】

👉 【LeetCode 直通車】:42 接雨水(困難)[24]

題解/***@param{number[]}height*@return{number}*/vartrap=function(height){letl_max=[],r_max=[];letlen=height.length;letmaxCapacity=0;for(leti=0;i<len;i++){l_max[i]=height[i];r_max[i]=height[i];}for(leti=1;i<len;i++){l_max[i]=Math.max(l_max[i-1],height[i]);}for(letj=len-2;j>=0;j--){r_max[j]=Math.max(r_max[j+1],height[j]);}for(leti=0;i<len;i++){maxCapacity+=Math.min(l_max[i],r_max[i])-height[i];}returnmaxCapacity;};跳躍遊戲【貪心算法】【系列】
👉 【LeetCode 直通車】:55 跳躍遊戲(中等)[25]
👉 【LeetCode 直通車】:45 跳躍遊戲 II(中等)[26]

受限於篇幅,這裡只給出第一道題的代碼模板,也是一面常考真題。

題解/***@param{number[]}nums*@return{boolean}*/varcanJump=function(nums){letfaster=0;for(leti=0;i<nums.length-1;i++){faster=Math.max(faster,i+nums[i]);if(faster<=i)returnfalse;}returnfaster>=nums.length-1;};高頻算法題系列:二叉樹

主要有以下幾類高頻考題:

二叉樹的最近公共祖先【簡單】【二叉樹】
二叉搜索樹中的搜索【簡單】【二叉樹】
刪除二叉搜索樹中的節點【中等】【二叉樹】
完全二叉樹的節點個數【中等】【二叉樹】
二叉樹的鋸齒形層序遍歷【中等】【二叉樹】
二叉樹的最近公共祖先【二叉樹】

👉 【LeetCode 直通車】:236 二叉樹的最近公共祖先(簡單)[27]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@param{TreeNode}p*@param{TreeNode}q*@return{TreeNode}*/letvisited;letparent;varlowestCommonAncestor=function(root,p,q){visited=newSet();parent=newMap();dfs(root);while(p!=null){visited.add(p.val);p=parent.get(p.val);}while(q!=null){if(visited.has(q.val)){returnq;}q=parent.get(q.val);}returnnull;};functiondfs(root){if(root.left!=null){parent.set(root.left.val,root);dfs(root.left);}if(root.right!=null){parent.set(root.right.val,root);dfs(root.right);}}二叉搜索樹中的搜索【二叉樹】

👉 【LeetCode 直通車】:700 二叉搜索樹中的搜索(簡單)[28]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@param{number}val*@return{TreeNode}*/varsearchBST=function(root,val){if(root==null)returnnull;if(root.val===val)returnroot;if(root.val>val){returnsearchBST(root.left,val);}elseif(root.val<val){returnsearchBST(root.right,val);}};刪除二叉搜索樹中的節點【二叉樹】

👉 【LeetCode 直通車】:450 刪除二叉搜索樹中的節點(中等)[29]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@param{number}key*@return{TreeNode}*/vardeleteNode=function(root,key){if(root==null)returnnull;if(root.val===key){if(root.left==null&&root.right==null)returnnull;if(root.left==null)returnroot.right;if(root.right==null)returnroot.left;if(root.left!=null&&root.right!=null){lettarget=getMinTreeMaxNode(root.left);root.val=target.val;root.left=deleteNode(root.left,target.val);}}if(root.val<key){root.right=deleteNode(root.right,key);}elseif(root.val>key){root.left=deleteNode(root.left,key);}returnroot;};functiongetMinTreeMaxNode(root){if(root.right==null)returnroot;returngetMinTreeMaxNode(root.right);}完全二叉樹的節點個數【二叉樹】

👉 【LeetCode 直通車】:222 完全二叉樹的節點個數(中等)[30]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@return{number}*/varcountNodes=function(root){if(root==null)return0;letl=root,r=root;letlh=0,rh=0;while(l!=null){l=l.left;lh++;}while(r!=null){r=r.right;rh++;}if(lh===rh){returnMath.pow(2,lh)-1;}return1+countNodes(root.left)+countNodes(root.right);};二叉樹的鋸齒形層序遍歷【二叉樹】

👉 【LeetCode 直通車】:103 二叉樹的鋸齒形層序遍歷(中等)[31]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@return{number[][]}*/letres;varzigzagLevelOrder=function(root){if(root==null)return[];res=[];BFS(root,true);returnres;};functionBFS(root,inOrder){letarr=[];letresItem=[];letnode;letstack1=newStack();letstack2=newStack();//判斷交換時機letflag;stack1.push(root);res.push([root.val]);inOrder=!inOrder;while(!stack1.isEmpty()||!stack2.isEmpty()){if(stack1.isEmpty()){flag='stack1';}elseif(stack2.isEmpty()){flag='stack2';}//決定取那個棧裡面的元素if(flag==='stack2'&&!stack1.isEmpty())node=stack1.pop();elseif(flag==='stack1'&&!stack2.isEmpty())node=stack2.pop();if(inOrder){if(node.left){if(flag==='stack1'){stack1.push(node.left);}else{stack2.push(node.left);}resItem.push(node.left.val);}if(node.right){if(flag==='stack1'){stack1.push(node.right);}else{stack2.push(node.right);}resItem.push(node.right.val);}}else{if(node.right){if(flag==='stack1'){stack1.push(node.right);}else{stack2.push(node.right);}resItem.push(node.right.val);}if(node.left){if(flag==='stack1'){stack1.push(node.left);}else{stack2.push(node.left);}resItem.push(node.left.val);}}//判斷下次翻轉的時機if((flag==='stack2'&&stack1.isEmpty())||(flag==='stack1'&&stack2.isEmpty())){inOrder=!inOrder;//需要翻轉了,就加一輪值if(resItem.length>0){res.push(resItem);}resItem=[];}}}classStack{constructor(){this.count=0;this.items=[];}push(element){this.items[this.count]=element;this.count++;}pop(){if(this.isEmpty())returnundefined;constelement=this.items[this.count-1];deletethis.items[this.count-1];this.count--;returnelement;}size(){returnthis.count;}isEmpty(){returnthis.size()===0;}}【🔥】高頻算法題系列:排序算法

主要有以下幾類高頻考題:

用最少數量的箭引爆氣球【中等】【排序】
合併區間【中等】【排序算法+區間問題】【面試真題】
用最少數量的箭引爆氣球【排序算法】

👉 【LeetCode 直通車】:452 用最少數量的箭引爆氣球(中等)[32]

題解/***@param{number[][]}points*@return{number}*/varfindMinArrowShots=function(points){if(points.length===0)return0;points.sort((a,b)=>a[1]-b[1]);letcnt=1;letresArr=[points[0]];letcurr,last;for(leti=1;i<points.length;i++){curr=points[i];last=resArr[resArr.length-1];if(curr[0]>last[1]){resArr.push(curr);cnt++;}}returncnt;};合併區間【排序算法+區間問題】

👉 【LeetCode 直通車】:56 合併區間(中等)[33]

題解/***@param{number[][]}intervals*@return{number[][]}*/varmerge=function(intervals){if(intervals.length===0)return[];intervals.sort((a,b)=>a[0]-b[0]);letmergeArr=[intervals[0]];letlast,curr;for(letj=1;j<intervals.length;j++){last=mergeArr[mergeArr.length-1];curr=intervals[j];if(last[1]>=curr[0]){last[1]=Math.max(curr[1],last[1]);}else{mergeArr.push(curr);}}returnmergeArr;};高頻算法題系列:二分查找

主要有以下幾類高頻考題:

尋找兩個正序數組的中位數【困難】【二分查找】
判斷子序列【簡單】【二分查找】
在排序數組中查找元素的第一個和最後一個位置【中等】【二分查找】
尋找兩個正序數組的中位數【二分查找】

👉 【LeetCode 直通車】:4 尋找兩個正序數組的中位數(困難)[34]

題解/***@param{number[]}nums1*@param{number[]}nums2*@return{number}*/varfindMedianSortedArrays=function(nums1,nums2){letm=nums1.length,n=nums2.length;leti=0,j=0;letnewArr=[];while(i<m&&j<n){if(nums1[i]<nums2[j]){newArr.push(nums1[i++]);}else{newArr.push(nums2[j++]);}}newArr=newArr.concat(i<m?nums1.slice(i):nums2.slice(j));constlen=newArr.length;console.log(newArr)if(len%2===0){return(newArr[len/2]+newArr[len/2-1])/2;}else{returnnewArr[Math.floor(len/2)];}};判斷子序列【二分查找】

👉 【LeetCode 直通車】:392 判斷子序列(簡單)[35]

題解/***@param{string}s*@param{string}t*@return{boolean}*/varisSubsequence=function(s,t){lethash={};for(leti=0;i<t.length;i++){if(!hash[t[i]])hash[t[i]]=[];hash[t[i]].push(i);}letlastMaxIndex=0;for(leti=0;i<s.length;i++){if(hash[s[i]]){constindex=binarySearch(hash[s[i]],lastMaxIndex);console.log('index',index,hash[s[i]]);if(index===-1)returnfalse;lastMaxIndex=hash[s[i]][index]+1;}elsereturnfalse;}returntrue;};functionbinarySearch(array,targetIndex){letleft=0,right=array.length;while(left<right){letmid=left+Math.floor((right-left)/2);if(array[mid]>=targetIndex){right=mid;}elseif(array[mid]<targetIndex){left=mid+1;}}if(left>=array.length||array[left]<targetIndex)return-1;returnleft;}💁 在排序數組中查找元素的第一個和最後一個位置【二分搜索】

👉 【LeetCode 直通車】:34 在排序數組中查找元素的第一個和最後一個位置(中等)[36]

題解/***@param{number[]}nums*@param{number}target*@return{number[]}*/varsearchRange=function(nums,target){constleft=leftBound(nums,target);constright=rightBound(nums,target);return[left,right];};functionleftBound(nums,target){letleft=0;letright=nums.length-1;while(left<=right){letmid=Math.floor(left+(right-left)/2);if(nums[mid]===target){right=mid-1;}elseif(nums[mid]<target){left=mid+1;}elseif(nums[mid]>target){right=mid-1;}}if(left>=nums.length||nums[left]!==target){return-1;}returnleft;}functionrightBound(nums,target){letleft=0;letright=nums.length-1;while(left<=right){letmid=Math.floor(left+(right-left)/2);if(nums[mid]===target){left=mid+1;}elseif(nums[mid]<target){left=mid+1;}elseif(nums[mid]>target){right=mid-1;}}if(right<0||nums[right]!==target){return-1;}returnright;}【🔥】高頻算法題系列:動態規劃

主要有以下幾類高頻考題:

最長遞增子序列【中等】【動態規劃】
零錢兌換【中等】【動態規劃】【面試真題】
最長公共子序列 【中等】【動態規劃】【面試真題】
編輯距離 【困難】【動態規劃】
最長回文子序列【中等】【動態規劃】【面試真題】
最大子序和【簡單】【動態規劃】【面試真題】
買賣股票的最佳時機系列【系列】【動態規劃】【面試真題】
最長遞增子序列【動態規劃】

👉 【LeetCode 直通車】:300 最長遞增子序列(中等)[37]

題解/***@param{number[]}nums*@return{number}*/varlengthOfLIS=function(nums){letmaxLen=0,n=nums.length;letdp=[];for(leti=0;i<n;i++){dp[i]=1;}for(leti=0;i<n;i++){for(letj=0;j<i;j++){if(nums[i]>nums[j]){dp[i]=Math.max(dp[i],dp[j]+1);}}maxLen=Math.max(maxLen,dp[i]);}returnmaxLen;};【面試真題】 零錢兌換【動態規劃】

👉 【LeetCode 直通車】:322 零錢兌換(中等)[38]

題解/***@param{number[]}coins*@param{number}amount*@return{number}*/varcoinChange=function(coins,amount){if(amount===0)return0;letdp=[];for(leti=0;i<=amount;i++){dp[i]=amount+1;}dp[0]=0;for(leti=0;i<=amount;i++){for(letj=0;j<coins.length;j++){if(i>=coins[j]){dp[i]=Math.min(dp[i-coins[j]]+1,dp[i])}}}returndp[amount]===amount+1?-1:dp[amount];};【面試真題】 最長公共子序列【動態規劃】

👉 【LeetCode 直通車】:1143 最長公共子序列(中等)[39]

題解/***@param{string}text1*@param{string}text2*@return{number}*/varlongestCommonSubsequence=function(text1,text2){letn1=text1.length,n2=text2.length;letdp=[];for(leti=-1;i<n1;i++){dp[i]=[];for(letj=-1;j<n2;j++){dp[i][j]=0;}}for(leti=0;i<n1;i++){for(letj=0;j<n2;j++){if(text1[i]===text2[j]){dp[i][j]=Math.max(dp[i][j],dp[i-1][j-1]+1);}else{dp[i][j]=Math.max(dp[i-1][j],dp[i][j-1])}}}returndp[n1-1][n2-1];};編輯距離【動態規劃】

👉 【LeetCode 直通車】:72 編輯距離(困難)[40]

題解/***@param{string}word1*@param{string}word2*@return{number}*/varminDistance=function(word1,word2){letlen1=word1.length,len2=word2.length;letdp=[];for(leti=0;i<=len1;i++){dp[i]=[];for(letj=0;j<=len2;j++){dp[i][j]=0;if(i===0){dp[i][j]=j;}if(j===0){dp[i][j]=i;}}}for(leti=1;i<=len1;i++){for(letj=1;j<=len2;j++){if(word1[i-1]===word2[j-1]){dp[i][j]=dp[i-1][j-1];}else{dp[i][j]=Math.min(dp[i-1][j]+1,dp[i][j-1]+1,dp[i-1][j-1]+1);}}}returndp[len1][len2];};【面試真題】最長回文子序列【動態規劃】

👉 【LeetCode 直通車】:516 最長回文子序列(中等)[41]

題解/***@param{string}s*@return{number}*/varlongestPalindromeSubseq=function(s){letdp=[];for(leti=0;i<s.length;i++){dp[i]=[];for(letj=0;j<s.length;j++){dp[i][j]=0;}dp[i][i]=1;}for(leti=s.length-1;i>=0;i--){for(letj=i+1;j<s.length;j++){if(s[i]===s[j]){dp[i][j]=dp[i+1][j-1]+2;}else{dp[i][j]=Math.max(dp[i+1][j],dp[i][j-1]);}}}returndp[0][s.length-1];};【面試真題】💁 最大子序和【動態規劃】

👉 【LeetCode 直通車】:53 最大子序和(簡單)[42]

題解/***@param{number[]}nums*@return{number}*/varmaxSubArray=function(nums){letmaxSum=-Infinity;letdp=[],n=nums.length;for(leti=-1;i<n;i++){dp[i]=0;}for(leti=0;i<n;i++){dp[i]=Math.max(nums[i],dp[i-1]+nums[i]);maxSum=Math.max(maxSum,dp[i]);}returnmaxSum;};【面試真題】💁 買賣股票的最佳時機【動態規劃】
👉 【LeetCode 直通車】:121 買賣股票的最佳時機(簡單)[43]【面試真題】
👉 【LeetCode 直通車】:122 買賣股票的最佳時機 II(簡單)[44]
👉 【LeetCode 直通車】:123 買賣股票的最佳時機 III(困難)[45]
👉 【LeetCode 直通車】:188 買賣股票的最佳時機IV(困難)[46]
👉 【LeetCode 直通車】:309 買賣股票的最佳時機含冷凍期(中等)[47]
👉 【LeetCode 直通車】:714 買賣股票的最佳時機含手續費(中等)[48]

受限於篇幅,這裡只給出第一道題的代碼模板,也是一面常考真題,筆者在面試字節跳動時就遇到過。

題解/***@param{number[]}prices*@return{number}*/varmaxProfit=function(prices){letdp=[];for(leti=-1;i<prices.length;i++){dp[i]=[]for(letj=0;j<=1;j++){dp[i][j]=[];dp[i][j][0]=0;dp[i][j][1]=0;if(i===-1){dp[i][j][1]=-Infinity;}if(j===0){dp[i][j][1]=-Infinity;}if(j===-1){dp[i][j][1]=-Infinity;}}}for(leti=0;i<prices.length;i++){for(letj=1;j<=1;j++){dp[i][j][0]=Math.max(dp[i-1][j][0],dp[i-1][j][1]+prices[i]);dp[i][j][1]=Math.max(dp[i-1][j][1],dp[i-1][j-1][0]-prices[i]);}}returndp[prices.length-1][1][0];};高頻算法題系列:BFS

主要有以下幾類高頻考題:

打開轉盤鎖【中等】【BFS】
二叉樹的最小深度【簡單】【BFS】
打開轉盤鎖【BFS】

👉 【LeetCode 直通車】:752 打開轉盤鎖(中等)[49]

題解/***@param{string[]}deadends*@param{string}target*@return{number}*/varopenLock=function(deadends,target){letqueue=newQueue();letvisited=newSet();letstep=0;queue.push('0000');visited.add('0000');while(!queue.isEmpty()){letsize=queue.size();for(leti=0;i<size;i++){letstr=queue.pop();if(deadends.includes(str))continue;if(target===str){returnstep;}for(letj=0;j<4;j++){letplusStr=plusOne(str,j);letminusStr=minusOne(str,j);if(!visited.has(plusStr)){queue.push(plusStr);visited.add(plusStr)}if(!visited.has(minusStr)){queue.push(minusStr);visited.add(minusStr)}}}step++;}return-1;};functionplusOne(str,index){letstrArr=str.split('');if(strArr[index]==='9'){strArr[index]='0'}else{strArr[index]=(Number(strArr[index])+1).toString()}returnstrArr.join('');}functionminusOne(str,index){letstrArr=str.split('');if(strArr[index]==='0'){strArr[index]='9'}else{strArr[index]=(Number(strArr[index])-1).toString()}returnstrArr.join('');}classQueue{constructor(){this.items=[];this.count=0;this.lowerCount=0;}push(elem){this.items[this.count++]=elem;}pop(){if(this.isEmpty()){return;}constelem=this.items[this.lowerCount];deletethis.items[this.lowerCount];this.lowerCount++;returnelem;}isEmpty(){if(this.size()===0)returntrue;returnfalse;}size(){returnthis.count-this.lowerCount;}}二叉樹的最小深度【BFS】

👉 【LeetCode 直通車】:111 二叉樹的最小深度(簡單)[50]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}root*@return{number}*/varminDepth=function(root){if(root==null)return0;letdepth=1;letqueue=newQueue();queue.push(root);while(!queue.isEmpty()){letsize=queue.size();for(leti=0;i<size;i++){constnode=queue.pop();if(node.left==null&&node.right==null)returndepth;if(node.left){queue.push(node.left);}if(node.right){queue.push(node.right);}}depth++;}returndepth;};classQueue{constructor(){this.items=[];this.count=0;this.lowerCount=0;}push(elem){this.items[this.count++]=elem;}pop(){if(this.isEmpty()){return;}constelem=this.items[this.lowerCount];deletethis.items[this.lowerCount];this.lowerCount++;returnelem;}isEmpty(){if(this.size()===0)returntrue;returnfalse;}size(){returnthis.count-this.lowerCount;}}【🔥】高頻算法題系列:棧

主要有以下幾類高頻考題:

最小棧【簡單】【棧】
有效的括號【中等】【棧】【面試真題】
簡化路徑【中等】【棧】
下一個更大元素 【系列】【棧】
最小棧【棧】

👉 【LeetCode 直通車】:155 最小棧(簡單)[51]

題解/***initializeyourdatastructurehere.*/varMinStack=function(){this.stack=[];this.minArr=[];this.count=0;this.min=Number.MAX_SAFE_INTEGER;};/***@param{number}x*@return{void}*/MinStack.prototype.push=function(x){this.min=Math.min(this.min,x);this.minArr[this.count]=this.min;this.stack[this.count]=x;this.count++;};/***@return{void}*/MinStack.prototype.pop=function(){constelement=this.stack[this.count-1];if(this.count-2>=0)this.min=this.minArr[this.count-2];elsethis.min=Number.MAX_SAFE_INTEGER;deletethis.stack[this.count-1];deletethis.minArr[this.count-1];this.count--;returnelement;};/***@return{number}*/MinStack.prototype.top=function(){if(this.count>=1){returnthis.stack[this.count-1];}returnnull;};/***@return{number}*/MinStack.prototype.getMin=function(){constelement=this.minArr[this.count-1];returnelement;};/***YourMinStackobjectwillbeinstantiatedandcalledassuch:*varobj=newMinStack()*obj.push(x)*obj.pop()*varparam_3=obj.top()*varparam_4=obj.getMin()*/【系列】下一個更大元素 【棧】
👉 【LeetCode 直通車】:496 下一個更大元素 I(簡單)[52]
👉 【LeetCode 直通車】:503 下一個更大元素 II(中等)[53]

受限於篇幅,這裡只給出第一道題的代碼模板

題解/***@param{number[]}nums*@return{number[]}*/varnextGreaterElements=function(nums){letans=[];letstack=newStack();constn=nums.length;for(leti=2*n-1;i>=0;i--){while(!stack.isEmpty()&&stack.top()<=nums[i%n]){stack.pop();}ans[i%n]=stack.isEmpty()?-1:stack.top();stack.push(nums[i%n]);}returnans;};classStack{constructor(){this.count=0;this.items=[];}top(){if(this.isEmpty())returnundefined;returnthis.items[this.count-1];}push(element){this.items[this.count]=element;this.count++;}pop(){if(this.isEmpty())returnundefined;constelement=this.items[this.count-1];deletethis.items[this.count-1];this.count--;returnelement;}isEmpty(){returnthis.size()===0;}size(){returnthis.count;}}【面試真題】有效的括號【棧】

👉 【LeetCode 直通車】:20 有效的括號(中等)[54]

題解/***@param{string}s*@return{boolean}*/varisValid=function(s){if(s.length===0){returntrue;}if(s.length%2!==0){returnfalse;}letmap={')':'(',']':'[','}':'{',};letleft=['(','[','{'];letright=[')',']','}'];letstack=newStack();for(leti=0;i<s.length;i++){if(!right.includes(s[i])){stack.push(s[i]);}else{constmatchStr=map[s[i]];while(!stack.isEmpty()){constelement=stack.pop();if(left.includes(element)&&matchStr!==element)returnfalse;if(element===matchStr)break;}}}returnstack.isEmpty();};classStack{constructor(){this.count=0;this.items=[];}push(element){this.items[this.count]=element;this.count++;}pop(){if(this.isEmpty())returnundefined;constelement=this.items[this.count-1];deletethis.items[this.count-1];this.count--;returnelement;}isEmpty(){returnthis.size()===0;}size(){returnthis.count;}}簡化路徑【棧】

👉 【LeetCode 直通車】:71 簡化路徑(中等)[55]

題解/***@param{string}path*@return{string}*/varsimplifyPath=function(path){letnewPath=path.split('/');newPath=newPath.filter(item=>item!=="");conststack=newStack();for(letsofnewPath){if(s==='..')stack.pop();elseif(s!=='.')stack.push(s);}if(stack.isEmpty())return'/';letstr='';while(!stack.isEmpty()){constelement=stack.pop();str='/'+element+str;}returnstr;};functionhandleBack(stack,tag,num){if(!stack.isEmpty())returnnum;constelement=stack.pop();if(element==='..')returnhandleBack(stack,tag,num+1);else{stack.push(element);returnnum;}}classStack{constructor(){this.count=0;this.items=[];}push(element){this.items[this.count]=element;this.count++;}pop(){if(this.isEmpty())returnundefined;constelement=this.items[this.count-1];deletethis.items[this.count-1];this.count--;returnelement;}size(){returnthis.count;}isEmpty(){returnthis.size()===0;}}【🔥】高頻算法題系列:DFS

主要有以下幾類高頻考題:

島嶼的最大面積【中等】【DFS】
相同的樹【簡單】【DFS】
島嶼的最大面積【DFS】

👉 【LeetCode 直通車】:695 島嶼的最大面積(中等)[56]

題解/***@param{number[][]}grid*@return{number}*/letmaxX,maxY;letvisited;letglobalMaxArea;varmaxAreaOfIsland=function(grid){visited=newSet();maxX=grid.length;maxY=grid[0].length;globalMaxArea=0;for(leti=0;i<maxX;i++){for(letj=0;j<maxY;j++){if(grid[i][j]===1){visited.add(`(${i},${j})`);globalMaxArea=Math.max(globalMaxArea,dfs(grid,i,j));}visited.clear();}}returnglobalMaxArea;};functiondfs(grid,x,y){letres=1;for(leti=-1;i<=1;i++){for(letj=-1;j<=1;j++){if(Math.abs(i)===Math.abs(j))continue;constnewX=x+i;constnewY=y+j;if(newX>=maxX||newX<0||newY>=maxY||newY<0)continue;if(visited.has(`(${newX},${newY})`))continue;visited.add(`(${newX},${newY})`);constareaCnt=grid[newX][newY]if(areaCnt===1){constcnt=dfs(grid,newX,newY);res+=cnt;}}}returnres;}相同的樹【DFS】

👉 【LeetCode 直通車】:100 相同的樹(簡單)[57]

題解/***Definitionforabinarytreenode.*functionTreeNode(val){*this.val=val;*this.left=this.right=null;*}*//***@param{TreeNode}p*@param{TreeNode}q*@return{boolean}*/varisSameTree=function(p,q){if(p==null&&q==null)returntrue;if(p==null||q==null)returnfalse;if(p.val!==q.val)returnfalse;returnisSameTree(p.left,q.left)&&isSameTree(p.right,q.right);};【🔥】高頻算法題系列:回溯算法

主要有以下幾類高頻考題:

N皇后【困難】【回溯算法】【面試真題】
全排列【中等】【回溯算法】
括號生成【中等】【回溯算法】
復原 IP 地址【中等】【回溯算法】
子集 【簡單】【回溯算法】
【面試真題】N皇后【回溯算法】

👉 【LeetCode 直通車】:51 N皇后(困難)[58]

題解/***@param{number}n*@return{string[][]}*/letresult=[];varsolveNQueens=function(n){result=[];letboard=[];for(leti=0;i<n;i++){board[i]=[];for(letj=0;j<n;j++){board[i][j]='.'}}backtrack(0,board,n);returnresult;};functiondeepClone(board){letres=[];for(leti=0;i<board.length;i++){res.push(board[i].join(''));}returnres;}functionbacktrack(row,board,n){if(row===n){result.push(deepClone(board));return;}for(letj=0;j<n;j++){if(checkInValid(board,row,j,n))continue;board[row][j]='Q';backtrack(row+1,board,n);board[row][j]='.';}}functioncheckInValid(board,row,column,n){//行for(leti=0;i<n;i++){if(board[i][column]==='Q')returntrue;}for(leti=row-1,j=column+1;i>=0&&j<n;i--,j++){if(board[i][j]==='Q')returntrue;}for(leti=row-1,j=column-1;i>=0&&j>=0;i--,j--){if(board[i][j]==='Q')returntrue;}returnfalse;}全排列【回溯算法】

👉 【LeetCode 直通車】:46 全排列(中等)[59]

題解/***@param{number[]}nums*@return{number[][]}*/letresults=[];varpermute=function(nums){results=[];backtrack(nums,[]);returnresults;};functionbacktrack(nums,track){if(nums.length===track.length){results.push(track.slice());return;}for(leti=0;i<nums.length;i++){if(track.includes(nums[i]))continue;track.push(nums[i]);backtrack(nums,track);track.pop();}}括號生成【回溯算法】

👉 【LeetCode 直通車】:22 括號生成(中等)[60]

題解/***@param{number}n*@return{string[]}*/vargenerateParenthesis=function(n){letvalidRes=[];backtrack(n*2,validRes,'');returnvalidRes;};functionbacktrack(len,validRes,bracket){if(bracket.length===len){if(isValidCombination(bracket)){validRes.push(bracket);}return;}for(letstrof['(',')']){bracket+=str;backtrack(len,validRes,bracket);bracket=bracket.slice(0,bracket.length-1);}}functionisValidCombination(bracket){letstack=newStack();for(leti=0;i<bracket.length;i++){conststr=bracket[i];if(str==='('){stack.push(str);}elseif(str===')'){consttop=stack.pop();if(top!=='(')returnfalse;}}returnstack.isEmpty();}classStack{constructor(){this.count=0;this.items=[];}push(element){this.items[this.count]=element;this.count++;}pop(){if(this.isEmpty())return;constelement=this.items[this.count-1];deletethis.items[this.count-1];this.count--;returnelement;}size(){returnthis.count;}isEmpty(){returnthis.size()===0;}}復原 IP 地址【回溯算法】

👉 【LeetCode 直通車】:93 復原 IP 地址(中等)[61]

題解/***@param{string}s*@return{string[]}*/varrestoreIpAddresses=function(s){if(s.length>12)return[];letres=[];consttrack=[];backtrack(s,track,res);returnres;};functionbacktrack(s,track,res){if(track.length===4&&s.length===0){res.push(track.join('.'));return;}letlen=s.length>=3?3:s.length;for(leti=0;i<len;i++){constc=s.slice(0,i+1);if(parseInt(c)>255)continue;if(i>=1&&parseInt(c)<parseInt((1+'0'.repeat(i))))continue;track.push(c);backtrack(s.slice(i+1),track,res);track.pop();}}子集【回溯算法】

👉 【LeetCode 直通車】:78 子集(中等)[62]

題解/***@param{number[]}nums*@return{number[][]}*/varsubsets=function(nums){if(nums.length===0)return[[]];letresArr=[];backtrack(nums,0,[],resArr);returnresArr;};functionbacktrack(nums,index,subArr,resArr){if(Array.isArray(subArr)){resArr.push(subArr.slice());}if(index===nums.length){return;}for(leti=index;i<nums.length;i++){subArr.push(nums[i]);backtrack(nums,i+1,subArr,resArr);subArr.pop(nums[i]);}}文末福利

推薦一個非常有幫助的刷算法題的網址,labuladong 的算法小抄[63],通過套路,認準高頻題目,直通大廠;這本小炒目前已經出版成書,對應的 Github 倉庫[64]也有 86.2K Star,而且作者還在頻繁更新,非常值得學習!

❤️謝謝往期精文
字節跳動最愛考的前端面試題:JavaScript 基礎[65] 2696 👍
字節跳動最愛考的前端面試題:CSS 基礎[66] 687 👍
字節跳動最愛考的前端面試題:計算機網絡基礎[67] 761 👍

歡迎關注公眾號:圖雀社區。 如果你想從零開始以實戰的方式學習一門技術,亦或是想動手做一個比較完整的項目以準備面試,相信 「圖雀社區」 的內容都能夠幫助到你,成為初入前端的你成長路上的指南針。

原創不易

喜歡的話原創不易,給點鼓勵吧 ❤️ 別忘了 分享、點讚、在看 三連哦~。

參考資料
[1]

【LeetCode 直通車】:234 回文鍊表(簡單): https://leetcode-cn.com/problems/palindrome-linked-list/

[2]

【LeetCode 直通車】:206 反轉鍊表(簡單): https://leetcode-cn.com/problems/reverse-linked-list/

[3]

【LeetCode 直通車】:23 合併K個升序鍊表(困難): https://leetcode-cn.com/problems/merge-k-sorted-lists/

[4]

【LeetCode 直通車】:25 K 個一組翻轉鍊表(困難): https://leetcode-cn.com/problems/reverse-nodes-in-k-group/

[5]

【LeetCode 直通車】:141 環形鍊表(簡單): https://leetcode-cn.com/problems/linked-list-cycle/

[6]

【LeetCode 直通車】:148 排序鍊表(中等): https://leetcode-cn.com/problems/sort-list/

[7]

【LeetCode 直通車】:160 相交鍊表(簡單): https://leetcode-cn.com/problems/intersection-of-two-linked-lists/

[8]

【LeetCode 直通車】:5 最長回文子串(中等): https://leetcode-cn.com/problems/longest-palindromic-substring/

[9]

【LeetCode 直通車】:14 最長公共前綴(簡單): https://leetcode-cn.com/problems/longest-common-prefix/

[10]

【LeetCode 直通車】:3 無重複字符的最長子串(中等): https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/

[11]

【LeetCode 直通車】:76 最小覆蓋子串(困難): https://leetcode-cn.com/problems/minimum-window-substring/

[12]

【LeetCode 直通車】:354 俄羅斯套娃信封問題(困難): https://leetcode-cn.com/problems/russian-doll-envelopes/

[13]

【LeetCode 直通車】:674 最長連續遞增序列(簡單): https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence/

[14]

【LeetCode 直通車】:128 最長連續序列(困難): https://leetcode-cn.com/problems/longest-consecutive-sequence/

[15]

【LeetCode 直通車】:11 盛最多水的容器(中等): https://leetcode-cn.com/problems/container-with-most-water/

[16]

【LeetCode 直通車】:4 尋找兩個正序數組的中位數(困難): https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

[17]

【LeetCode 直通車】:26 刪除有序數組中的重複項(簡單): https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/

[18]

【LeetCode 直通車】:695 島嶼的最大面積(中等): https://leetcode-cn.com/problems/max-area-of-island/

[19]

【LeetCode 直通車】:560 和為K的子數組(中等): https://leetcode-cn.com/problems/subarray-sum-equals-k/

[20]

【LeetCode 直通車】:1 兩數之和(簡單): https://leetcode-cn.com/problems/two-sum/

[21]

【LeetCode 直通車】:167 兩數之和 II - 輸入有序數組(簡單): https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted/

[22]

【LeetCode 直通車】:15 三數之和(中等): https://leetcode-cn.com/problems/3sum/

[23]

【LeetCode 直通車】:18 四數之和(中等): https://leetcode-cn.com/problems/4sum/

[24]

【LeetCode 直通車】:42 接雨水(困難): https://leetcode-cn.com/problems/trapping-rain-water/

[25]

【LeetCode 直通車】:55 跳躍遊戲(中等): https://leetcode-cn.com/problems/jump-game/

[26]

【LeetCode 直通車】:45 跳躍遊戲 II(中等): https://leetcode-cn.com/problems/jump-game-ii/

[27]

【LeetCode 直通車】:236 二叉樹的最近公共祖先(簡單): https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/

[28]

【LeetCode 直通車】:700 二叉搜索樹中的搜索(簡單): https://leetcode-cn.com/problems/search-in-a-binary-search-tree/

[29]

【LeetCode 直通車】:450 刪除二叉搜索樹中的節點(中等): https://leetcode-cn.com/problems/delete-node-in-a-bst/

[30]

【LeetCode 直通車】:222 完全二叉樹的節點個數(中等): https://leetcode-cn.com/problems/count-complete-tree-nodes/

[31]

【LeetCode 直通車】:103 二叉樹的鋸齒形層序遍歷(中等): https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/

[32]

【LeetCode 直通車】:452 用最少數量的箭引爆氣球(中等): https://leetcode-cn.com/problems/minimum-number-of-arrows-to-burst-balloons/

[33]

【LeetCode 直通車】:56 合併區間(中等): https://leetcode-cn.com/problems/merge-intervals/

[34]

【LeetCode 直通車】:4 尋找兩個正序數組的中位數(困難): https://leetcode-cn.com/problems/median-of-two-sorted-arrays/

[35]

【LeetCode 直通車】:392 判斷子序列(簡單): https://leetcode-cn.com/problems/is-subsequence/

[36]

【LeetCode 直通車】:34 在排序數組中查找元素的第一個和最後一個位置(中等): https://leetcode-cn.com/problems/find-first-and-last-position-of-element-in-sorted-array/

[37]

【LeetCode 直通車】:300 最長遞增子序列(中等): https://leetcode-cn.com/problems/longest-increasing-subsequence/

[38]

【LeetCode 直通車】:322 零錢兌換(中等): https://leetcode-cn.com/problems/coin-change/

[39]

【LeetCode 直通車】:1143 最長公共子序列(中等): https://leetcode-cn.com/problems/longest-common-subsequence/

[40]

【LeetCode 直通車】:72 編輯距離(困難): https://leetcode-cn.com/problems/edit-distance/

[41]

【LeetCode 直通車】:516 最長回文子序列(中等): https://leetcode-cn.com/problems/longest-palindromic-subsequence/

[42]

【LeetCode 直通車】:53 最大子序和(簡單): https://leetcode-cn.com/problems/maximum-subarray/

[43]

【LeetCode 直通車】:121 買賣股票的最佳時機(簡單): https://leetcode-cn.com/problems/container-with-most-water/

[44]

【LeetCode 直通車】:122 買賣股票的最佳時機 II(簡單): https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/

[45]

【LeetCode 直通車】:123 買賣股票的最佳時機 III(困難): https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/

[46]

【LeetCode 直通車】:188 買賣股票的最佳時機IV(困難): https://leetcode-cn.com/problems/container-with-most-water/

[47]

【LeetCode 直通車】:309 買賣股票的最佳時機含冷凍期(中等): https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/

[48]

【LeetCode 直通車】:714 買賣股票的最佳時機含手續費(中等): https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/

[49]

【LeetCode 直通車】:752 打開轉盤鎖(中等): https://leetcode-cn.com/problems/open-the-lock/

[50]

【LeetCode 直通車】:111 二叉樹的最小深度(簡單): https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/

[51]

【LeetCode 直通車】:155 最小棧(簡單): https://leetcode-cn.com/problems/min-stack/submissions/

[52]

【LeetCode 直通車】:496 下一個更大元素 I(簡單): https://leetcode-cn.com/problems/next-greater-element-i/

[53]

【LeetCode 直通車】:503 下一個更大元素 II(中等): https://leetcode-cn.com/problems/next-greater-element-ii/

[54]

【LeetCode 直通車】:20 有效的括號(中等): https://leetcode-cn.com/problems/valid-parentheses/

[55]

【LeetCode 直通車】:71 簡化路徑(中等): https://leetcode-cn.com/problems/simplify-path/

[56]

【LeetCode 直通車】:695 島嶼的最大面積(中等): https://leetcode-cn.com/problems/max-area-of-island/

[57]

【LeetCode 直通車】:100 相同的樹(簡單): https://leetcode-cn.com/problems/same-tree/

[58]

【LeetCode 直通車】:51 N皇后(困難): https://leetcode-cn.com/problems/n-queens/

[59]

【LeetCode 直通車】:46 全排列(中等): https://leetcode-cn.com/problems/permutations/

[60]

【LeetCode 直通車】:22 括號生成(中等): https://leetcode-cn.com/problems/generate-parentheses/

[61]

【LeetCode 直通車】:93 復原 IP 地址(中等): https://leetcode-cn.com/problems/restore-ip-addresses/

[62]

【LeetCode 直通車】:78 子集(中等): https://leetcode-cn.com/problems/subsets/

[63]

labuladong 的算法小抄: https://www.yuque.com/tuture/interview/labuladong:https

[64]

Github 倉庫: https://github.com/labuladong/fucking-algorithm

[65]

字節跳動最愛考的前端面試題:JavaScript 基礎: https://juejin.cn/post/6934500357091360781

[66]

字節跳動最愛考的前端面試題:CSS 基礎: https://juejin.cn/post/6936913689115099143

[67]

字節跳動最愛考的前端面試題:計算機網絡基礎: https://juejin.cn/post/6939691851746279437



在看點這裡
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 鑽石舞台 的頭像
    鑽石舞台

    鑽石舞台

    鑽石舞台 發表在 痞客邦 留言(0) 人氣()