LeetCode(面試)中寫出乾淨的程式碼(小技巧合集)

Tech In Pieces發表於2020-12-16

COMMON TIPS FOR CLEAN CODE
1 Calculate sum of a range quickly --using pre sum array
2 Move in four directions for a matrix --注意邊界 用4 way dfs
3 Split string by multiple separators --?LeetCode: Brace Expansion
4 Add a dummy tailing element to simplify code -?LeetCode: Brace Expansion
5 Fast slow pointers–這個是很常見的技巧 就不多說了
6 Deep copy an array --?LeetCode: Combination Sum
7 Use arrays instead of hashmaps, if possible – 這個也是常見技巧 但是自己很少注意 因為雖然能看懂 但是寫的時候多加了一層 費腦子
8 Control the order of dfs --?LeetCode: Subsets II
9 Avoid inserting into the head of an array --這個是常用的技巧,比如說我們zigzag level order.我們可以正常level order只是要加一個flag
10 From right to left, instead of left to right --這個技巧之前應該用過 但是不太記得什麼典型的題目了 LeetCode: Merge Sorted Array?
11 Think the other way around --說的輕巧 Add Items vs Remove Items, Increase Counter vs Decrease Counter(這個counter的之前還見過不少 因為我都死腦筋從0加到n 而沒有自己想過從n減到0)
12 Avoid unnecessary if…else… --就是說 多用用lambda表示式
13 To get the case of K, solve: at most K – at most (K-1) --要學會轉化問題 用逆遞進的方式來解決問題 LeetCode: Subarrays with K Different Integers
14 Instead of deleting entry from hashmap, decrease counter --沒有明白是什麼意思?counter只能對元素個數進行實時更新 而如果我們仍要對hashmap裡面元素進行取用的時候 那之前不刪除的話會影響結果的啊
15 Find the max/min; If not found, return 0 --? LeetCode: Minimum Area Rectangle
16 With helper function vs without helper function --意思就是 能拆成單獨模組的最好要拆 這樣體現出來你清晰的思路 LeetCode: Longest Repeating Character Replacement
17 Instead of adding a character, try to delete one --?這特麼是啥意思?LeetCode: Longest String Chain
18 #roudtrippass: from left to right, then right to left --?LeetCode: Shortest Distance to a Character
19 Delayed calculation to simplify the code --? LeetCode: Interval List Intersections
20 Instead of removing, add padding elements --? LeetCode: Duplicate Zeros
21 Initialize array with n+1 length to simplify code --這個在DP裡面經常用 在某些非DP問題裡面也經常用 LeetCode: Range Addition
22 Look for off-by-one errors, sometimes use i+1<len(l) vs i<len(l) --?這個i具體停在哪裡不是由題目決定的嗎?LeetCode: Previous Permutation With One Swap
23 Hashmap can reduce calculation, but may complicate things too --?這話說的倒沒錯 但是它易於理解 有什麼錯嗎?LeetCode: Maximum Frequency Stack
24 Sliding window to get the longest size of subarray --常見的sliding window技巧 LeetCode: Max Consecutive Ones III
25 In matrix dfs, change cell to impossible value to avoid state hashmap --?說什麼錘子呢?LeetCode: Word Search II
26 For palindrome check, check the whole string, instead of left half --? LeetCode: Longest Chunked Palindrome Decomposition
27 Use queue to keep flipping the orders LeetCode: Zigzag Iterator
28 Find a pair with sum meets some requirements --?這也能叫技巧?LeetCode: Two Sum
29 Add a dummy head node for linked list --這個就很常見了 一旦涉及到頭部處理變化 就必須dummy node LeetCode: Reverse Linked List
30 When count sort, use one array instead of two LeetCode: Minimum Number of Steps to Make Two Strings Anagram
31 Hide details which are irrelevant --?說什麼抽象話呢?怎麼隱藏這種東西?
32 One pass instead of two pass --有些follow up會要求我們只遍歷一遍 但是這算不上技巧吧 更像是屁話 我能遍歷一遍我會遍歷兩遍?
33 Avoid unnecessary precheck --簡直屁話 沒有必要我會Precheck?
34 Reduce search space Leetcode: Bulb Switcher II

相關文章