随笔分类 -  LeetCode Solutions

上一页 1 ··· 3 4 5 6 7 8 9 10 11 12 下一页
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... 阅读全文
posted @ 2015-07-14 12:13 jianchao-li 阅读(249) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-14 02:22 jianchao-li 阅读(240) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-13 23:10 jianchao-li 阅读(1733) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-13 15:36 jianchao-li 阅读(173) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-12 13:57 jianchao-li 阅读(166) 评论(0) 推荐(0)
摘要: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. ... 阅读全文
posted @ 2015-07-11 23:12 jianchao-li 阅读(164) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-11 22:38 jianchao-li 阅读(223) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-11 21:21 jianchao-li 阅读(356) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-11 18:12 jianchao-li 阅读(239) 评论(0) 推荐(0)
摘要: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 ... 阅读全文
posted @ 2015-07-11 18:00 jianchao-li 阅读(181) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-11 17:00 jianchao-li 阅读(205) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-11 14:37 jianchao-li 阅读(218) 评论(0) 推荐(0)
摘要:Well, remember to take advantage of the property of binary search trees, which is,node -> left -> val val right -> val. Moreover, bothpandqwill be t... 阅读全文
posted @ 2015-07-11 13:50 jianchao-li 阅读(384) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-10 20:40 jianchao-li 阅读(166) 评论(0) 推荐(0)
摘要: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 ... 阅读全文
posted @ 2015-07-10 13:40 jianchao-li 阅读(418) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-08 14:20 jianchao-li 阅读(161) 评论(0) 推荐(0)
摘要: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]... 阅读全文
posted @ 2015-07-08 01:50 jianchao-li 阅读(614) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-07 21:11 jianchao-li 阅读(218) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-07 12:46 jianchao-li 阅读(184) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-07 11:54 jianchao-li 阅读(283) 评论(0) 推荐(0)

上一页 1 ··· 3 4 5 6 7 8 9 10 11 12 下一页