Last Updated on 2024-01-04 by Clay
寫在開頭
LeetCode 是什麼?
LeetCode 是一個收集了各式各樣程式題目的網站,雖然說是各式各樣的題目,但大致上是面向著『面試白板題』導向的題目;換言之,有許多的程式設計師在就職前,都免不了反覆刷 LeetCode 上的題目。
當然,我個人也希望能夠藉由反覆練習 LeetCode 網站上的題目來幫助自己能夠順利地面試,於是便將自己解題時的想法、作法,通通一並記錄在這裡,權當作個參考。
另一方面,有時候解題時只是胡亂試出來罷了。若能整理自己的想法後紀錄下來 —— 雖不敢說就此融會貫通,但想必是多少真正了解了這個題目。
解題的指標
在 LeetCode 中解題,有著非常明顯的兩個指標:
- 使用的時間
- 使用的空間
與此同時,這兩種指標的呈現方式是與之前提交過的使用者們做比較,所以程式的效率好不好,基本是一目瞭然的。
不過講是這樣講,我卻仍舊覺得這兩種指標僅僅是個參考,也不要過度執著於此。畢竟有時候,會遇到執行效率很快、但是記憶體使用率卻超大的時候;反之,也會有著執行效率稱不上最快、但卻能節省不少記憶體使用空間的例子。要用哪種架構,也是端看程式設計時的需求做選擇。
除此之外,還有『可讀性』的問題存在,如果今天一項產品的程式碼並不存在時間複雜度、空間複雜度的瓶頸,並且還有著後續交接的預定,那麼,我們該犧牲可讀性、完全一昧地追求最高效能嗎?
以上的例子有些偏頗,不過,我覺得所謂『優化最貴的資源』就是這麼一回事吧!最貴的資源,在不同的時空背景下,都可能有所變化的。
可貴的,終究是在漫長的解題過程中,慢慢鍛鍊出來的『解決問題的思考能力』。
與所有要一同刷題的朋友,共勉之。
目錄
- 1-Two Sum
- 2-Add Two Numbers
- 3-Longest Substring Without Repeating Characters
- 4-Median of Two Sorted Arrays
- 5-Longest Palindromic Substring
- 6-ZigZag Conversion
- 7-Reverse Integer
- 8-String to Integer (atoi)
- 9-Palindrome Number
- 11-Container With Most Water
- 12-Integer to Roman
- 13-Roman to Integer
- 14-Longest Common Prefix
- 15-3Sum
- 16-3Sum Closest
- 18-4Sum
- 19-Remove Nth Node From End of List
- 20-Valid Parentheses
- 21-Merge Two Sorted Lists
- 22-Generate Parentheses
- 23-Merge k Sorted Lists
- 24-Swap Nodes in Pairs
- 31-Next Permutation
- 32-Longest Valid Parentheses
- 33-Search in Rotated Sorted Array
- 36-Valid Sudoku
- 39-Combination Sum
- 54-Maximum Subarray
- 55-Jump Game
- 67-Add Binary
- 70-Climbing Stairs
- 71-Simplify Path
- 74-Search a 2D Matrix
- 78-Subsets
- 81-Search in Rotated Sorted Array II
- 82-Remove Duplicates from Sorted List II
- 94-Binary Tree Inorder Traversal
- 100-Same Tree
- 121-Best Time to Buy and Sell Stock
- 124-Binary Tree Maximum Path Sum
- 133-Clone Graph
- 134-Gas Station
- 136-Single Number
- 138-Copy List with Random Pointer
- 141-Linked List Cycle
- 142-Linked List Cycle II
- 144-Binary Tree Preorder Traversal
- 150-Evaluate Reverse Polish Notation
- 168-Excel Sheet Column Title
- 169-Majority Element
- 171-Excel Sheet Column Number
- 198-House Robber
- 223-Rectangle Area
- 230-Kth Smallest Element in a BST
- 242-Valid Anagram
- 263-Ugly Number
- 290-Word Pattern
- 309-Best Time to Buy and Sell Stock with Cooldown
- 316-Remove Duplicate Letters
- 319-Bulb Switcher
- 328-Odd Even Linked List
- 344-Reverse String
- 380-Insert Delete GetRandom O(1)
- 382-Linked List Random Node
- 383-Counting Bits
- 389-Find the Difference
- 393-UTF-8 Validation
- 402-Remove K Digits
- 410-Split Array Largest Sum
- 446-Arithmetic Slices II – Subsequence
- 451-Sort Characters By Frequency
- 452-Minimum Number of Arrows to Burst Balloons
- 520-Detect Capital
- 567-Permutation in String
- 581-Shortest Unsorted Continuous Subarray
- 605-Can Place Flowers
- 606-Construct String from Binary Tree
- 609-Find Duplicated File in System
- 680-Valid Palindrome II
- 682-Baseball Game
- 701-Insert into Binary Search Tree
- 704-Binary Search
- 739-Daily Temperatures
- 763-Partition Labels
- 797-All Paths From Source to Target
- 841-Keys and Rooms
- 844-Backspace String Compare
- 849-Maximize Distance to Closest Person
- 856-Score of Parentheses
- 872-Leaf-Similar Trees
- 875-Koko Eating Bananas
- 876-Middle of the Linked List
- 881-Boats to Save People
- 895-Maximum Frequency Stack
- 897-Increasing Order Search Tree
- 905-Sort Array By Parity
- 931-Minimum Falling Path Sum
- 938-Range Sum of BST
- 941-Valid Mountain Array
- 944-Delete Columns to Make Sorted
- 946-Validate Stack Sequences
- 948-Bag of Tokens
- 985-Sum of Even Numbers After Queries
- 991-Broken Calculator
- 997-Find the Town Judge
- 1903-Largest Odd Number in String
- 1007-Minimum Domino Rotations For Equal Row
- 1010-Pairs of Songs With Total Durations Divisible by 60
- 1022-Sum of Root To Leaf Binary Numbers
- 1026. Maximum Difference Between Node and Ancestor
- 1029-Two City Scheduling
- 1046-Last Stone Weight
- 1071-Greatest Common Divisor of Strings
- 1094-Car Pooling
- 1217-Minimum Cost to Move Chips to The Same Position
- 1249-Minimum Remove to Make Valid Parentheses
- 1288-Remove Covered Intervals
- 1290-Convert Binary Number in a Linked List to Integer
- 1305-All Elements in Two Binary Search Trees
- 1337-The K Weakest Rows in a Matrix
- 1339-Maximum Product of Splitted Binary Tree
- 1431-Kids With the Greatest Number of Candies
- 1436-Destination City
- 1443-Minimum Time to Collect All Apples in a Tree
- 1446-Consecutive Characters
- 1457-Pseudo-Palindromic Paths in a Binary Tree
- 1470-Shuffle the Array
- 1510-Stone Game IV
- 1539-Kth Missing Positive Number
- 1557-Minimum Number of Vertices to Reach All Nodes
- 1561-Maximum Number of Coins You Can Get
- 1658-Minimum Operations to Reduce X to Zero
- 1663-Smallest String With A Given Numeric Value
- 1768-Merge Strings Alternately
- 1857-Largest Color Value in a Directed Graph
- 1921-Eliminate Maximum Number of Monsters
- 1926-Nearest Exit from Entrance in Maze
- 1962-Remove Stones to Minimize
- 1971-Find if Path Exists in Graph
- 2007-Find Original Array From Doubled Array
- 2149-Rearrange Array Elements by Sign
- 2225-Find Players With Zero or One Losses
- 2244-Minimum Rounds to Complete All Tasks
- 2256-Minimum Average Difference
- 2279-Maximum Bags With Full Capacity of Rocks
- 2336-Smallest Number in Infinite Set
- 2348-Number of Zero-Filled Subarrays
- 2352-Design a Food Rating System
- 2390-Removing Stars From a String
- 2391-Minimum Amount of Time to Collect Garbage
- 2485-Find the Pivot Integer
- 2486-Append Characters to String to Make Subsequence
- 2706-Buy Two Chocolates
- 2870-Minimum Number of Operations to Make Array Empty