/interview

总结一下面试常考的算法题,希望可以帮助每一位想要提升自己面试能力的同学。对于每一道算法题会总结代码、时间复杂度以及一些好的blog

ambition

总结一下面试常考的算法题

希望可以帮助每一位想要提升自己面试能力的同学。

对于每一道算法题会总结代码、时间复杂度以及一些好的blog


排序(sort)

LintCode 463 Sort Integers

LintCode 464 Sort Integers II

LeetCode 148 Sort List

LeetCode 215 Kth Largest Element in an Array

LintCode 532 Reverse Pairs

LeetCode 315 Count of Smaller Numbers After Self

LeetCode 207 Course Schedule

LeetCode 210 Course Schedule II

数组(array)

LeetCode 28 Implement strStr()

LeetCode 1 Two Sum

LeetCode 189 Rotate Array

LintCode 31 Partition Array

LintCode 373 Partition Array by Odd and Even

LintCode 144 Interleaving Positive and Negative Numbers

LeetCode 54 Spiral Matrix

LeetCode 59 Spiral Matrix II

LeetCode 53 Maximum Subarray

LeetCode 152 Maximum Product Subarray

LintCode 138 Subarray Sum

LintCode 139 Subarray Sum Closest

LeetCode 392 Is Subsequence

LeetCode 136 Single Number

LeetCode 137 Single Number II

LeetCode 260 Single Number III

LeetCode 263 Ugly Number

LeetCode 264 Ugly Number II

LeetCode 295 Find Median from Data Stream——hard

LeetCode 4 Median of Two Sorted Arrays——very hard

LeetCode 239 Sliding Window Maximum——hard

LeetCode 480 Sliding Window Median——hard

LintCode 130 Heapify——medium

LeetCode 347 Top K Frequent Elements——medium

LeetCode 128 Longest Consecutive Sequence——hard

双指针(two pointers)

LeetCode 15 3Sum——medium

LeetCode 16 3Sum Closest——medium

LeetCode 18 4Sum——medium

LeetCode 42 Trapping Rain Water——hard

链表(linkedList)

注意以下两点:
1、当你预估到返回的链表头结点可能跟原有的链表头节点不一样时,建一个虚拟节点dummy,值任意,比如0,最后返回的新的链表头结点就是dummy.next,这一条非常好用!
2、当操作一个链表节点的时候,时刻想一想要访问的链表节点是否为null

LeetCode 206 Reverse Linked List——easy

LeetCode 86 Partition List——medium

LeetCode 92 Reverse Linked List II——medium

LeetCode 83 Remove Duplicates from Sorted List——easy

LeetCode 82 Remove Duplicates from Sorted List II——medium

LeetCode 237 Delete Node in a Linked List——easy

LeetCode 19 Remove Nth Node From End of List——medium

LeetCode 61 Rotate List——medium

LeetCode 203 Remove Linked List Elements——easy

LeetCode 2 Add Two Numbers——medium

LeetCode 445 Add Two Numbers II——medium

LeetCode 328 Odd Even Linked List

LeetCode 234 Palindrome Linked List

LeetCode 24 Swap Nodes in Pairs

LeetCode 160 Intersection of Two Linked Lists

LeetCode 141 Linked List Cycle——easy

LeetCode 142 Linked List Cycle II——medium

LeetCode 21 Merge Two Sorted Lists——easy

LeetCode 23 Merge k Sorted Lists——hard

LeetCode 138 Copy List with Random Pointer——medium

LeetCode 143 Reorder List——medium

LeetCode 25 Reverse Nodes in k-Group——hard

LeetCode 146 LRU Cache——hard

二分(binary search)

需要注意的点有:
1、start 、end在while循环的条件、到底是start<end、还是start<=end;要仔细举例确认
2、还有mid值的计算 mid = start + (end-start)/2; 有时候为了跳出循环;可以 mid = start + (end-start)/2 + 1;

LeetCode 35 Search Insert Position——easy

LeetCode 34 Find First and Last Position of Element in Sorted Array——medium

LeetCode 278 First Bad Version——easy

LeetCode 162 Find Peak Element——medium

LeetCode 154 Find Minimum in Rotated Sorted Array II——hard

LeetCode 153 Find Minimum in Rotated Sorted Array——medium

LeetCode 240 Search a 2D Matrix II——medium

LeetCode 74 Search a 2D Matrix——medium

LeetCode 33 Search in Rotated Sorted Array——medium

LeetCode 81 Search in Rotated Sorted Array II——medium

LeetCode 378 Kth Smallest Element in a Sorted Matrix——medium

LeetCode 4 Median of Two Sorted Arrays

栈(stack)

LeetCode 155 Min Stack——easy

LeetCode 232 Implement Queue using Stacks——easy

LeetCode 225 Implement Stack using Queues——easy

LeetCode 84 Largest Rectangle in Histogram——hard

LeetCode 394 Decode String——medium

LeetCode 85 Maximal Rectangle——hard

广度优先搜索(BFS)

LeetCode 103 Binary Tree Zigzag Level Order Traversal——medium

LeetCode 102 Binary Tree Level Order Traversal——medium

LeetCode 107 Binary Tree Level Order Traversal II——easy

深度优先搜索(DFS)

LeetCode 200 Number of Islands——medium

LeetCode 144 Binary Tree Preorder Traversal——medium

LeetCode 94 Binary Tree Inorder Traversal——medium

LeetCode 145 Binary Tree Postorder Traversal——hard

LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal——medium

LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal——medium

LeetCode 110 Balanced Binary Tree——easy

LeetCode 98 Validate Binary Search Tree——medium

LeetCode 236 Lowest Common Ancestor of a Binary Tree——medium

LeetCode 104 Maximum Depth of Binary Tree——easy

LeetCode 111 Minimum Depth of Binary Tree——easy

LeetCode 257 Binary Tree Paths——easy

LeetCode 112 Path Sum——easy

LeetCode 113 Path Sum II——medium

LeetCode 437 Path Sum III——easy

LeetCode 114 Flatten Binary Tree to Linked List——medium

LeetCode 101 Symmetric Tree——easy

LintCode 375 Clone Binary Tree——easy

LintCode 595 Binary Tree Longest Consecutive Sequence——easy

LintCode 614 Binary Tree Longest Consecutive Sequence II——medium

LeetCode 572 Subtree of Another Tree——easy

leetcode 226. Invert Binary Tree——easy

LeetCode 124 Binary Tree Maximum Path Sum——hard

LeetCode 109 Convert Sorted List to Binary Search Tree——medium

LintCode 378 Convert Binary Search Tree to Doubly Linked List——medium

回溯法(backtracking)

LeetCode 494 Target Sum——medium

LeetCode 78 Subsets——medium

LeetCode 90 Subsets II——medium

LeetCode 39 Combination Sum——medium

LeetCode 40 Combination Sum II——medium

LeetCode 46 Permutations——medium

LeetCode 47 Permutations II——medium

LeetCode 131 Palindrome Partitioning——medium

动态规划(dynamic programming)

LeetCode 70 Climbing Stairs——easy

LeetCode 62 Unique Paths——medium

LeetCode 63 Unique Paths II——medium

LeetCode 64 Minimum Path Sum——medium

LeetCode 120 Triangle——medium

LeetCode 198 House Robber——easy

LeetCode 213 House Robber II——medium

LeetCode 279 Perfect Squares——medium

LeetCode 221 Maximal Square——medium

LeetCode 300 Longest Increasing Subsequence——medium

LeetCode 5 Longest Palindromic Substring——medium

LeetCode 115 Distinct Subsequences——hard

LeetCode 322 Coin Change——medium

LeetCode 354 Russian Doll Envelopes——hard

LeetCode 403 Frog Jump——hard

LeetCode 72 Edit Distance——hard

LeetCode 368 Largest Divisible Subset——medium

LeetCode 10 Regular Expression Matching——hard

LeetCode 329 Longest Increasing Path in a Matrix——hard

LeetCode 97 Interleaving String——hard

贪心(greedy)

贪心算法考的非常少,面试中非常少见,因为贪心算法的数学证明是很难的,短时间你不太可能证明出来,而且面试官也大概率证不出来,只有机试中才小概率会出贪心算法题,比如头条这种。所以做了以下这几个题就行。具体原因可以看一下这个链接:https://www.jiuzhang.com/qa/2100/

LintCode 46 Majority Element——easy

LeetCode 55 Jump Game——medium

LeetCode 45 Jump Game II——hard

LeetCode 134 Gas Station——medium

数学(math)

一些trick题,考察数学和逻辑推理

LintCode 1 A + B Problem——easy

LintCode 365 Count 1 in Binary——easy

LintCode 3 Digit Counts——medium

LintCode 379 Reorder array to construct the minimum number——medium