首页 > ACM题库 > LeetCode > LeetCode-分类题解[全]
2015
11-29

LeetCode-分类题解[全]

参考电子书:https://github.com/soulmachine/leetcode/raw/master/C%2B%2B/leetcode-cpp.pdf

原作者:戴方勤([email protected])

本书的目标读者是准备去北美找工作的码农,也适用于在国内找工作的码农,以及刚接触ACM 算法竞赛的新手。本书包含了LeetCode Online Judge(http://leetcode.com/onlinejudge) 所有题目的答案,所有代码经过精心编写,编码规范良好,适合读者反复揣摩,模仿,甚至在纸上默写。全书的代码,使用C++ 11 /JDK7的编写,并在LeetCode Online Judge 上测试通过。本书中的代码规范,跟在公司中的工程规范略有不同,为了使代码短(方便迅速实现): 所有代码都是单一文件。这是因为一般OJ 网站,提交代码的时候只有一个文本框,如果还是按照标准做法,比如分为头文件.h 和源代码.cpp,无法在网站上提交;Shorter is better。能递归则一定不用栈;能用STL 则一定不自己实现。不提倡防御式编程。不需要检查malloc()/new 返回的指针是否为nullptr;不需要检查内部函数入口参数的有效性。本手册假定读者已经学过《数据结构》《算法》- 这两门课,熟练掌握C++ 或Java。

补充:在原作者的基础上,对部分题目我又添加了Java或python的解题代码。

数组

Remove Duplicates from Sorted Array  Remove Duplicates from Sorted Array II  Search in Rotated Sorted Array  Search in Rotated Sorted Array II  Median of Two Sorted Arrays  Longest Consecutive Sequence  Two Sum  3Sum  3Sum Closest  4Sum  Remove Element  Next Permutation  Permutation Sequence  Valid Sudoku  Trapping Rain Water  Rotate Image  Plus One  Climbing Stairs  Gray Code  Set Matrix Zeroes  Gas Station  Candy  Single Number  Single Number II

链表

Add Two Numbers  Reverse Linked List II  Partition List  Remove Duplicates from Sorted List  Remove Duplicates from Sorted List II  Rotate List  Remove Nth Node From End of List  Swap Nodes in Pairs  Reverse Nodes in k-Group  Copy List with Random Pointer  Linked List Cycle  Linked List Cycle II  Reorder List  LRU Cache

字符串

Valid Palindrome  Implement strStr()  String to Integer (atoi)  Add Binary  Longest Palindromic Substring  Regular Expression Matching  Wildcard Matching  Longest Common Prefix  Valid Number  Integer to Roman  Roman to Integer  Count and Say  Anagrams  Simplify Path  Length of Last Word

Valid Parentheses  Longest Valid Parentheses  Largest Rectangle in Histogram  Evaluate Reverse Polish Notation

二叉树

Binary Tree Preorder Traversal  Binary Tree Inorder Traversal  Binary Tree Postorder Traversal  Binary Tree Level Order Traversal  Binary Tree Level Order Traversal II  Binary Tree Zigzag Level Order Traversal  Recover Binary Search Tree  Same Tree  Symmetric Tree  Balanced Binary Tree  Flatten Binary Tree to Linked List  Populating Next Right Pointers in Each Node II  Construct Binary Tree from Preorder and Inorder Traversal  Construct Binary Tree from Inorder and Postorder Traversal  Unique Binary Search Trees  Unique Binary Search Trees II  Validate Binary Search Tree  Convert Sorted Array to Binary Search Tree  Convert Sorted List to Binary Search Tree  Minimum Depth of Binary Tree  Maximum Depth of Binary Tree  Path Sum  Path Sum II  Binary Tree Maximum Path Sum  Populating Next Right Pointers in Each Node  Sum Root to Leaf Numbers

排序

Merge Sorted Array  Merge Two Sorted Lists  Merge k Sorted Lists  Insertion Sort List  Sort List  First Missing Positive  Sort Colors

查找

Search for a Range  Search Insert Position  Search a 2D Matrix

暴力枚举

Subsets  Subsets II  Permutations  Permutations II  Combinations  Letter Combinations of a Phone Number

BFS

Word Ladder  Word Ladder II  Surrounded Regions  BFS小结

DFS

Palindrome Partitioning  Unique Paths  Unique Paths II  N-Queens  N-Queens II  Restore IP Addresses  Combination Sum  Combination Sum II  Generate Parentheses  Sudoku Solver  Word Search

分治

Pow(x, n)  Sqrt(x)

贪心

Jump Game  Jump Game II  Best Time to Buy and Sell Stock  Best Time to Buy and Sell Stock II  Longest Substring Without Repeating Characters  Container With Most Water

动态规划

Triangle  Maximum Subarray  Palindrome Partitioning II  Maximal Rectangle  Best Time to Buy and Sell Stock III  Interleaving String  Scramble String  Minimum Path Sum  Edit Distance  Decode Ways  Distinct Subsequences  Word Break  Word Break II

Clone Graph

模拟

Reverse Integer  Palindrome Number  Insert Interval  Merge Intervals  Minimum Window Substring  Multiply Strings  Substring with Concatenation of All Words  Pascal’s Triangle  Pascal’s Triangle II  Spiral Matrix  Spiral Matrix II  ZigZag Conversion  Divide Two Integers  Text Justification  Max Points on a Line


  1. 代码是给出了,但是解析的也太不清晰了吧!如 13 abejkcfghid jkebfghicda
    第一步拆分为 三部分 (bejk, cfghi, d) * C(13,3),为什么要这样拆分,原则是什么?