LeetCode(面试)中写出干净的代码(小技巧合集)

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

posted @ 2020-12-16 01:53  EvanMeetTheWorld  阅读(48)  评论(0)    收藏  举报