随笔分类 - LeetCode Solutions
Solutions to problems on https://leetcode.com/problemset/algorithms/.
摘要:Problem DescriptionGiven a string, find the length of the longest substring T that contains at most 2 distinct characters.For example, Given s =“eceba...
阅读全文
摘要:The key to this problem is to identify all the words that can be added to a line and then identify the places that require an additional space (to mak...
阅读全文
摘要:An interesting problem! But not very easy at first glance. You need to think very clear about how will a keypoint be generated. Since I only learn fro...
阅读全文
摘要:Well, a follow-up for the problemLowest Common Ancestor of a Binary Search Tree. However, this time you cannot figure out which subtree the given node...
阅读全文
摘要:Use the strs[0] as the reference string and then compare it with the remaining strings from left to right. Once we find a string with length less than...
阅读全文
摘要:Well, a typical backtracking problem. The code is as follows. You may walk through it using the example in the problem statement to see how it works. ...
阅读全文
摘要:This problem has a very easy recursive code as follows. 1 class Solution { 2 public: 3 bool hasPathSum(TreeNode* root, int sum) { 4 if (!r...
阅读全文
摘要:This problem has a naive idea, which is to traverse all possible pairs of two points and see how many other points fall in the line determined by them...
阅读全文
摘要:A simple application of level-order traversal. Just push the last node in each level into the result.The code is as follows. 1 class Solution { 2 publ...
阅读全文
摘要:The problem becomes more difficult once the binary tree is not perfect. The idea is still similar to use a level-order traversal. Note that we do not ...
阅读全文
摘要:The idea is similar to a level-order traversal and remember to take full advantages of the prefect binary tree assumption in the problem statement.The...
阅读全文
摘要:I guess some of you may have noticed that this seemingly simple problem has the lowest acceptance rate among all problems on the LeetCode OJ. Well, so...
阅读全文
摘要:Well, remember to take advantage of the property of binary search trees, which is,node -> left -> val val right -> val. Moreover, bothpandqwill be t...
阅读全文
摘要:The suggested solution to this problem has given a clear idea. The tricky part of this problem is to handle all the edge cases carefully and write a c...
阅读全文
摘要:The idea is not so obvious at first glance. Since you cannot move from a node back to its previous node in a singly linked list, we choose to reverse ...
阅读全文
摘要:The key to this problem is to store the values in a stack. In the constructor and next, we add all the next smallest nodes into the stack. The followi...
阅读全文
摘要:The following idea is taken from a book named 《剑指offer》 published in China.Supposen = 271, it then breaks[1, 271]into[1, 71]and[72, 271]. For[72, 271]...
阅读全文
摘要:This is a classic problem of linked list. You may solve it using two pointers. The tricky part lies in the head pointer may also be the one that is re...
阅读全文
摘要:This link has a nice explanation of the problem. I rewrite the code below. Play with it using some special examples and you will find out how it works...
阅读全文
摘要:A classic interview question. This link has a nice explanation of the idea using two stacks and its amortized time complexity.I use the same idea in m...
阅读全文

浙公网安备 33010602011771号