Skip to content

guangliang2014/leetcode

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode

###LeetCode Algorithm

(Notes: "♥" means you need buy a book from Leetcode)

# Title Solution Difficulty
223 Sliding Window Maximum C++ Hard
222 Product of Array Except Self C++ Medium
221 Delete Node in a Linked List C++ Easy
220 Lowest Common Ancestor of a Binary Tree C++ Medium
219 Lowest Common Ancestor of a Binary Search Tree C++ Easy
218 Palindrome Linked List C++ Easy
217 Number of Digit One C++ Medium
216 Implement Queue using Stacks C++ Easy
215 Power of Two C++ Easy
214 Kth Smallest Element in a BST C++ Medium
213 Majority Element II C++ Medium
212 Summary Ranges C++ Easy
211 Basic Calculator II C++ Medium
210 Invert Binary Tree C++ Easy
209 Implement Stack using Queues C++ Medium
208 Basic Calculator C++ Medium
207 Rectangle Area C++ Easy
206 Count Complete Tree Nodes C++ Medium
205 Maximal Square C++ Medium
204 Contains Duplicate III C++ Medium
203 Contains Duplicate II C++ Easy
202 The Skyline Problem C++ Hard
201 Contains Duplicate C++ Easy
200 Combination Sum III C++ Medium
199 Kth Largest Element in an Array C++ Medium
198 Shortest Palindrome C++ Hard
197 House Robber II C++ Medium
196 Word Search II C++ Hard
195 Add and Search Word - Data structure design C++ Medium
194 Course Schedule II C++ Medium
193 Minimum Size Subarray Sum C++ Medium
192 Implement Trie (Prefix Tree) C++ Medium
191 Course Schedule C++ Medium
190 Reverse Linked List C++ Easy
189 Isomorphic Strings C++ Easy
188 Count Primes C++ Easy
187 Remove Linked List Elements C++ Easy
186 Happy Number C++ Easy
185 Bitwise AND of Numbers Range C++ Medium
184 Number of Islands C++ Medium
183 Binary Tree Right Side View C++ Medium
182 House Robber C++ Easy
181 Number of 1 Bits C++ Easy
180 Reverse Bits C++ Easy
179 Rotate Array C++ Easy
178 Best Time to Buy and Sell Stock IV C++ Hard
177 Repeated DNA Sequences C++ Medium
176 Reverse Words in a String II C++ Medium
175 Largest Number C++ Medium
174 Dungeon Game C++ Hard
173 Binary Search Tree Iterator C++ Medium
172 Factorial Trailing Zeroes C++ Easy
171 Excel Sheet Column Number C++ Easy
170 Two Sum III - Data structure design C++ Easy
169 Majority Element C++ Easy
168 Excel Sheet Column Title C++ Easy
167 Two Sum II - Input array is sorted C++ Medium
166 Fraction to Recurring Decimal C++ Medium
165 Compare Version Numbers C++ Easy
164 Maximum Gap C++ Hard
163 Missing Ranges C++ Medium
162 Find Peak Element C++ Medium
161 One Edit Distance C++ Medium
160 Intersection of Two Linked Lists C++ Easy
159 Longest Substring with At Most Two Distinct Characters C++ Hard
158 Read N Characters Given Read4 II - Call multiple times C++ Hard
157 Read N Characters Given Read4 C++ Easy
156 Binary Tree Upside Down C++ Medium
155 Min Stack C++ Easy
154 Find Minimum in Rotated Sorted Array II C++ Hard
153 Find Minimum in Rotated Sorted Array C++ Medium
152 Maximum Product Subarray C++ Medium
151 Reverse Words in a String C++ Medium
150 Evaluate Reverse Polish Notation C++ Medium
149 Max Points on a Line C++ Hard
148 Sort List C++ Medium
147 Insertion Sort List C++ Medium
146 LRU Cache C++ Hard
145 Binary Tree Postorder Traversal C++ Hard
144 Binary Tree Preorder Traversal C++ Medium
143 Reorder List C++ Medium
142 Linked List Cycle II C++ Medium
141 Linked List Cycle C++ Medium
140 Word Break II C++ Hard
139 Word Break C++ Medium
138 Copy List with Random Pointer C++ Hard
137 Single Number II C++ Medium
136 Single Number C++ Medium
135 Candy C++ Hard
134 Gas Station C++ Medium
133 Clone Graph C++ Medium
132 Palindrome Partitioning II C++ Hard
131 Palindrome Partitioning C++ Medium
130 Surrounded Regions C++ Medium
129 Sum Root to Leaf Numbers C++ Medium
128 Longest Consecutive Sequence C++ Hard
127 Word Ladder II C++ Hard
126 Word Ladder C++ Medium
125 Valid Palindrome C++ Easy
124 Binary Tree Maximum Path Sum C++ Hard
123 Best Time to Buy and Sell Stock III C++ Hard
122 Best Time to Buy and Sell Stock II C++ Medium
121 Best Time to Buy and Sell Stock C++ Medium
120 Triangle C++ Medium
119 Pascal's Triangle II C++ Easy
118 Pascal's Triangle C++ Easy
117 Populating Next Right Pointers in Each Node II C++ Hard
116 Populating Next Right Pointers in Each Node C++ Medium
115 Distinct Subsequences C++ Hard
114 Flatten Binary Tree to Linked List C++ Medium
113 Path Sum II C++ Medium
112 Path Sum C++ Easy
111 Minimum Depth of Binary Tree C++ Easy
110 Balanced Binary Tree C++ Easy
109 Convert Sorted List to Binary Search Tree C++ Medium
108 Convert Sorted Array to Binary Search Tree C++ Medium
107 Binary Tree Level Order Traversal II C++ Easy
106 Construct Binary Tree from Inorder and Postorder Traversal C++ Medium
105 Construct Binary Tree from Preorder and Inorder Traversal C++ Medium
104 Maximum Depth of Binary Tree C++ Easy
103 Binary Tree Zigzag Level Order Traversal C++ Medium
102 Binary Tree Level Order Traversal C++ Easy
101 Symmetric Tree C++ Easy
100 Same Tree C++ Easy
99 Recover Binary Search Tree C++ Hard
98 Validate Binary Search Tree C++ Medium
97 Interleaving String C++ Hard
96 Unique Binary Search Trees II C++ Medium
95 Unique Binary Search Trees C++ Medium
94 Binary Tree Inorder Traversal C++ Medium
93 Restore IP Addresses C++ Medium
92 Reverse Linked List II C++ Medium
91 Subsets II C++ Medium
90 Decode Ways C++ Medium
89 Gray Code C++ Medium
88 Merge Sorted Array C++ Easy
87 Scramble String C++ Hard
86 Partition List C++ Medium
85 Maximal Rectangle C++ Hard
84 Largest Rectangle in Histogram C++ Hard
83 Remove Duplicates from Sorted List II C++ Medium
82 Remove Duplicates from Sorted List C++ Easy
81 Search in Rotated Sorted Array II C++ Medium
80 Remove Duplicates from Sorted Array II C++ Medium
79 Word Search C++ Medium
78 Subsets C++ Medium
77 Combinations C++ Medium
76 Minimum Window Substring C++ Hard
75 Sort Colors C++ Medium
74 Search a 2D Matrix C++ Medium
73 Set Matrix Zeroes C++ Medium
72 Edit Distance C++ Hard
71 Simplify Path C++ Medium
70 Climbing Stairs C++ Easy
69 Sqrt(x) C++ Medium
68 Text Justification C++ Hard
67 Plus One C++ Easy
66 Valid Number C++ Easy
65 Add Binary C++ Easy
64 Merge Two Sorted Lists C++ Easy
63 Minimum Path Sum C++ Medium
62 Unique Paths II C++ Medium
61 Unique Paths C++ Medium
60 Rotate List C++ Medium
59 Permutation Sequence C++ Medium
58 Spiral Matrix II C++ Medium
57 Length of Last Word C++ Easy
56 Insert Interval C++ Hard
55 Merge Intervals C++ Hard
54 Jump Game C++ Medium
53 Spiral Matrix C++ Medium
52 Maximum Subarray C++ Medium
51 N-Queens II C++ Hard
50 N-Queens C++ Hard
49 "Pow(x, n)" C++ Medium
48 Anagrams C++ Medium
47 Rotate Image C++ Medium
46 Permutations II C++ Hard
45 Permutations C++ Medium
44 Jump Game II C++ Hard
43 Wildcard Matching C++ Hard
42 Multiply Strings C++ Medium
41 Trapping Rain Water C++ Hard
40 First Missing Positive C++ Hard
39 Combination Sum II C++ Medium
38 Combination Sum C++ Medium
37 Count and Say C++ Easy
36 Sudoku Solver C++ Hard
35 Valid Sudoku C++ Easy
34 Search Insert Position C++ Medium
33 Search for a Range C++ Medium
32 Search in Rotated Sorted Array C++ Hard
31 Longest Valid Parentheses C++ Hard
30 Next Permutation C++ Medium
29 Substring with Concatenation of All Words C++ Hard
28 Divide Two Integers C++ Medium
27 Implement strStr() C++ Easy
26 Remove Element C++ Easy
25 Remove Duplicates from Sorted Array C++ Easy
24 Reverse Nodes in k-Group C++ Hard
23 Swap Nodes in Pairs C++ Medium
22 Merge k Sorted Lists C++ Hard
21 Generate Parentheses C++ Medium
20 Valid Parentheses C++ Easy
19 Remove Nth Node From End of List C++ Easy
18 Letter Combinations of a Phone Number C++ Medium
17 4Sum C++ Medium
16 3Sum Closest C++ Medium
15 3Sum C++ Medium
14 Longest Common Prefix C++ Easy
13 Roman to Integer C++ Easy
12 Integer to Roman C++ Medium
11 Container With Most Water C++ Medium
10 Regular Expression Matching C++ Hard
9 Palindrome Number C++ Easy
8 String to Integer (atoi) C++ Easy
7 Reverse Integer C++ Easy
6 ZigZag Conversion C++ Easy
5 Longest Palindromic Substring C++ Medium
4 Add Two Numbers C++ Medium
3 Longest Substring Without Repeating Characters C++ Medium
2 Median of Two Sorted Arrays C++ Hard
1 Two Sum C++ Medium

###LeetCode Shell

# Title Solution Difficulty
4 Tenth Line Bash Easy
3 Transpose File Bash Medium
2 Valid Phone Numbers Bash Easy
1 Word Frequency Bash Medium

###如何写无bug的程序是程序的晋级关键! ###原作者针对如何写无bug的程序的建议:处理好程序的状态,避免case-by-case的的情况。

Leetcode这个网站上的题都是一些经典的公司用来面试应聘者的面试题,很多人通过刷这些题来应聘一些喜欢面试算法的公司,比如:Google、微软、Facebook、Amazon之类的这些公司,基本上是应试教育的功利主义。

我做这些题目的不是为了要去应聘这些公司,而是为了锻炼一下自己的算法和编程能力。因为我开始工作的时候基本没有这样的训练算法和编程的网站,除了大学里的“算法和数据结构”里的好些最基础最基础的知识,基本上没有什么训练。所以,当我看到有人在做这些题的时候,我也蠢蠢欲动地想去刷一下。

于是,我花了3-4个月的业余时间,我把Leetcode的154道题全部做完了。(这也是最近我没有太多的时间来写博客的原因,你可以看到我之前做的那个活动中有几个算法题来自于Leetcode)有人说我时间太多了,这里声明一下,我基本上都是利用了晚上10点以后的时间来做这些题的。

LeetCode的题大致分成两类:

1)基础算法的知识。这些题里面有大量的算法题,解这些题都是有套路的,不是用递归(深度优先DFS,广度优先BFS),就是要用动态规划(Dynamic Programming),或是拆半查找(Binary Search),或是回溯(Back tracing),或是分治法(Divide and Conquer),还有大量的对树,数组、链表、字符串和hash表的操作。通过做这些题能让你对这些最基础的算法的思路有非常扎实的了解和训练。对我而言,Dynamic Programming 是我的短板,尤其是一些比较复杂的问题,在推导递推公式上总是有思维的缺陷(数学是我的硬伤),通过做了这些题后,我能感到我在DP的思路上有了很大的收获。

2)编程题。比如:atoi,strstr,add two num,括号匹配,字符串乘法,通配符匹配,文件路径简化,Text Justification,反转单词等等,这些题的Edge Case, Corner Case有很多。这些题需要你想清楚了再干,只要你稍有疏忽,就会有几个case让你痛不欲生,而且一不小心就会让你的代码会写得又臭又长,无法阅读。通过做这些题,可以非常好的训练你对各种情况的考虑,以及你对程序代码组织的掌控(其实就是其中的状态变量)。还记得我在《函数式编程》中说的,程序中的状态是你程序变得复杂难维护的直接原因。

我觉得每个程序员都应该花时间和精力做这些题,因为你会从这些题中得到很大的收益。做完这些题后你一定会明白下面几个道理:

1)想清楚了再干。这个观点我以前就在《多些时间可以少些代码》说过。如果你拿到题就上去直接写代码的话,你一定会被各种case打回来了。然后呢,你一着急,你就会进入那种我在《开发团队的效率》中说的那种毫无效率case by case的开发模式,而你也进入了“平庸模式”。于是你就会出现下图那样的情况。 Case-by-Case Developement

Case-by-Case Development

  1. 编程是脑力劳动,急不得。这个事情在这做这些题的时候你就会发现,要么是脑子转不过来了,要么就是明明就差一点了,但程序怎么都调不对。如果你越着急的话,你就会发现你会离目标越远,而花的时间也会更多。另外,你会发现这些题基本上都是50行代码内就可以搞定的,但是为了这50行以内的代码,你要花好多时间和精力。coding 50行代码在我们的日常工作中分分钟就完成,而Leetcode里的50行代码却没那么简单,也许,用这个你就可以区别什么是码农,什么是程序员了。

3)加班要不得。因为我总是在晚上10点以后做题,所以,基本上都是在加班状态中工作。这种状态过上两三天,你就会发现,整个大脑已经不转了,而且不但不转,还会犯很多低级错误,很多事情都想不清楚,一个晚上都在和程序的状态控制做搏斗,代码写得越来越乱,越来越没条理。于是这种时候,我都会休息几天,不做题了,然后再做题的时候,就觉得非常地清楚。可见加班 是编程最致命的敌人!

我把我的C++代码放到了Github上,大家也帮我review一下,看看有没有可以改善的。

https://github.com/haoel/leetcode

好了,不多说了,我希望大家有时间都去练练LeetCode,无论是找工作还是对你的编程能力会有非常大的提高。

(全文完)

About

LeetCode Problems' Solutions (up-to-date)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 98.6%
  • Shell 1.4%