由於樹狀結構並不像 Lined List 或陣列那樣是線狀的,故需要遍歷整個樹狀結構是很複雜的,而且有多種方式。 大致上分為以下兩種: Breadth-fir...
146. LRU Cache Question Design a data structure that follows the constraints of...
Bucket Sort 和之前的 Radix Sort 有點類似,建立幾個桶子並將資料丟進去排序。而 Bucket Sort 是取區間,例如 1 號桶子裝 0...
9. Palindrome Number Question Given an integer x, return true if x is palindrome...
在這篇之前的排序法都可以用在任何可以比較的資料上,例如一個含有帳戶資料的陣列,按照每個帳戶的 ID 、更新時間、名字、帳戶餘額等等來排序。但 Radix Sor...
198. House Robber Question You are a professional robber planning to rob houses...
14. Longest Common Prefix Question Write a function to find the longest common p...
72. Edit Distance Question Given two strings word1 and word2, return the minimum...
121. Best Time to Buy and Sell Stock Question You are given an array prices wher...
24. Swap Nodes in Pairs Solution 1: Recursive class Solution: def swapPairs(...
300. Longest Increasing Subsequence Question Given an integer array nums, return...
70. Climbing Stairs Question You are climbing a staircase. It takes n steps to r...
Quick Sort 使用基準值 (pivot) 比對排序,並透過 Recursion 的技巧,不斷將每個元素放到正確的位置上。 第一步是從陣列中取出一個數字當...
Merge Sort 是一種透過切分資料再一一合併的排序演算法。 Merge Sort 有使用到 Divide and Conquer 與 Recursion...
21. Merge Two Sorted Lists Question You are given the heads of two sorted linked...
206. Reverse Linked List Question Given the head of a singly linked list, revers...
22. Generate Parentheses Question Given n pairs of parentheses, write a function...
136. Single Number Question Given a non-empty array of integers nums, every elem...
從第二個元素開始,往前比對,如果比前一個元素小,則交換位置,以此類推。 以 [30, 5, 1, 31, 10, 9, 2, 3, 4, 8, 7, 6] 來說...
Selection Sort 實作上是遍歷一次陣列,找出最小值,並將最小值與陣列的第一個值交換,以此類推,再遍歷一次陣列 (先前排序好的位置可以略過) 找出最小...
26. Remove Duplicates from Sorted Array Question Given an integer array nums sor...
10. Regular Expression Matching Question Given an input string s and a pattern p...
20. Valid Parentheses Question Given a string s containing just the characters '...
42. Trapping Rain Water Question Given n non-negative integers representing an e...
Linear Search Linear Search 非常常見,甚至在學迴圈時就已經用過了。以下直接給範例練習! Practice - Linear Sear...
Recursion 的定義是一個會呼叫自己的函式。 Recursion 技巧在很多地方都有用到,例如: JSON.stringify & JSON....
將一組資料切分成兩組或多組資料,再用切分後的資料進行處理。此技巧能有效減少時間複雜度。 此技巧大量用於搜尋演算法內,以下用 二分搜尋法 (Binary Sear...
Sliding Window 跟上篇 Multiple Pointers 類似,定義兩個指標,一個是 start,一個是 end。 像是: start...
11. Container With Most Water Question You are given an integer array height of...
53. Maximum Subarray Question Given an integer array nums, find the contiguous s...