07 2015 档案

摘要:Well, this problem seems to be a little tricky at first glance. However, the idea (taken from this link) is really simple. Let's take the equation 2*3... 阅读全文
posted @ 2015-07-27 23:32 jianchao-li 阅读(423) 评论(0) 推荐(0) 编辑
摘要:Well, you may need to run some examples to have the intuition for the answer since we only require children with higher rating get more candies than t... 阅读全文
posted @ 2015-07-27 00:32 jianchao-li 阅读(164) 评论(0) 推荐(0) 编辑
摘要:Well, a dynamic programming problem. Let's first define its statedp[i][j]to be the number of distinct subsequences oft[0..i - 1]ins[0..j - 1]. Then we... 阅读全文
posted @ 2015-07-26 12:44 jianchao-li 阅读(235) 评论(0) 推荐(0) 编辑
摘要:Well, the idea is to search from thetop-rightelement and then reduce the range for further searching by comparisons betweentargetand the current eleme... 阅读全文
posted @ 2015-07-25 12:11 jianchao-li 阅读(215) 评论(0) 推荐(0) 编辑
摘要:This problem is more or less the same asFind Minimum in Rotated Sorted Array. And one key difference is as stated in the solution tag. That is, due to... 阅读全文
posted @ 2015-07-19 17:57 jianchao-li 阅读(179) 评论(0) 推荐(0) 编辑
摘要:As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers:lfor the left andrfor the right. One key i... 阅读全文
posted @ 2015-07-19 15:57 jianchao-li 阅读(320) 评论(0) 推荐(0) 编辑
摘要:For those who have already solvedSearch in Rotated Sorted Array, this problem can be solved similarly using codes for that problem and simply adding c... 阅读全文
posted @ 2015-07-19 14:17 jianchao-li 阅读(272) 评论(0) 推荐(0) 编辑
摘要:This problem is a nice application of binary search. The key lies in how to determine the correct half fortarget. Since the array has been rotated, we... 阅读全文
posted @ 2015-07-18 22:22 jianchao-li 阅读(215) 评论(0) 推荐(0) 编辑
摘要:Well, in the case of a linked list instead of an array, the problem becomes easier. We just need to find the node that will be the new head of the lis... 阅读全文
posted @ 2015-07-17 07:39 jianchao-li 阅读(167) 评论(0) 推荐(0) 编辑
摘要:This problem, as stated in the problem statement, has a lot of solutions. Since the problem requires us to solve it in O(1) space complexity, I only s... 阅读全文
posted @ 2015-07-17 07:04 jianchao-li 阅读(163) 评论(0) 推荐(0) 编辑
摘要:The first answer in this link has a nice illustrative explanation.Suppose the given array is like [nums[0], nums[1], nums[2], nums[3]]. Then the resul... 阅读全文
posted @ 2015-07-16 14:38 jianchao-li 阅读(377) 评论(0) 推荐(0) 编辑
摘要:As the note in the problem statement, this problem has a straight-forward O(n)-space solution, which is to generate the inorder traversal results of t... 阅读全文
posted @ 2015-07-15 23:27 jianchao-li 阅读(208) 评论(0) 推荐(0) 编辑
摘要:This problem has a nice BFS structure. Let's illustrate it using the examplenums = [2, 3, 1, 1, 4]in the problem statement. We are initially at positi... 阅读全文
posted @ 2015-07-15 21:30 jianchao-li 阅读(220) 评论(0) 推荐(0) 编辑
摘要:This problem has a very concise solution in this link, just 4-lines. The code is rewritten below.1 class Solution {2 public:3 bool canJump(vector&... 阅读全文
posted @ 2015-07-15 20:20 jianchao-li 阅读(210) 评论(0) 推荐(0) 编辑
摘要:Well, this problem is just a trick. In fact, we cannot really delete the given node, but just delete its next node after copying the data of the next ... 阅读全文
posted @ 2015-07-15 13:44 jianchao-li 阅读(264) 评论(0) 推荐(0) 编辑
摘要:Note: If you feel unwilling to read the long codes, just take the idea with you. The codes are unnecessarily long due to the inconvenient handle of ma... 阅读全文
posted @ 2015-07-15 00:42 jianchao-li 阅读(254) 评论(0) 推荐(0) 编辑
摘要:Note: The following idea is in fact from the last answer inthis link, which leads to a clean code. I just reorganize it and add some explanations. I h... 阅读全文
posted @ 2015-07-14 23:55 jianchao-li 阅读(401) 评论(0) 推荐(0) 编辑
摘要:Well, an interesting problem. If you draw some examples on a white board and try to figure out the regularities, you may have noticed that the key to ... 阅读全文
posted @ 2015-07-14 21:35 jianchao-li 阅读(233) 评论(0) 推荐(0) 编辑
摘要:Well, since we need to make a deep copy of the list and nodes in the list have arandom pointer that may point to any node in the list (or NULL), we ne... 阅读全文
posted @ 2015-07-14 12:35 jianchao-li 阅读(206) 评论(0) 推荐(0) 编辑
摘要: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 阅读(242) 评论(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 阅读(235) 评论(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 阅读(1723) 评论(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 阅读(170) 评论(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 阅读(162) 评论(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 阅读(159) 评论(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 阅读(216) 评论(0) 推荐(0) 编辑
摘要:1 class Solution { 2 public: 3 /** 4 * @param nums: a vector of integers 5 * @return: an integer 6 */ 7 int findMissing(ve... 阅读全文
posted @ 2015-07-11 21:32 jianchao-li 阅读(261) 评论(0) 推荐(0) 编辑
摘要:1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(int a, int b) ... 阅读全文
posted @ 2015-07-11 21:28 jianchao-li 阅读(1106) 评论(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 阅读(353) 评论(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 阅读(235) 评论(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 阅读(175) 评论(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 阅读(199) 评论(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 阅读(213) 评论(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 阅读(377) 评论(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 阅读(161) 评论(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 阅读(412) 评论(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 阅读(156) 评论(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 阅读(605) 评论(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 阅读(215) 评论(0) 推荐(0) 编辑
摘要:1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int val) { 8 * ... 阅读全文
posted @ 2015-07-07 21:09 jianchao-li 阅读(214) 评论(0) 推荐(0) 编辑
摘要:1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int val) { 8 * ... 阅读全文
posted @ 2015-07-07 20:03 jianchao-li 阅读(183) 评论(0) 推荐(0) 编辑
摘要:1 class Solution { 2 public: 3 /** 4 * @param A: a vector of integers 5 * @return: an integer 6 */ 7 int firstMissingPosit... 阅读全文
posted @ 2015-07-07 16:10 jianchao-li 阅读(257) 评论(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 阅读(178) 评论(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 阅读(276) 评论(0) 推荐(0) 编辑
摘要:递归实现: 1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers. 5 * @return: A list of unique permutations. 6 */ 7 ... 阅读全文
posted @ 2015-07-07 01:23 jianchao-li 阅读(891) 评论(0) 推荐(0) 编辑
摘要:递归实现:class Solution {public: /** * @param nums: A list of integers. * @return: A list of permutations. */ vector > permute(vector nu... 阅读全文
posted @ 2015-07-07 01:15 jianchao-li 阅读(345) 评论(0) 推荐(0) 编辑
摘要:I think the following code is self-explanatory enough. We use anunordered_map countsto record the expected times of each word and anotherunordered_map... 阅读全文
posted @ 2015-07-06 22:32 jianchao-li 阅读(216) 评论(0) 推荐(0) 编辑
摘要:By far the best solution I have seen is of O(n) time (some solutions claim to be of O(logn)turns out to be O(n)). One of the simplest ideas is to comp... 阅读全文
posted @ 2015-07-06 18:16 jianchao-li 阅读(228) 评论(0) 推荐(0) 编辑
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ... 阅读全文
posted @ 2015-07-06 15:08 jianchao-li 阅读(167) 评论(0) 推荐(0) 编辑
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ... 阅读全文
posted @ 2015-07-06 14:54 jianchao-li 阅读(253) 评论(0) 推荐(0) 编辑
摘要:Well, since theheadpointer may also been modified, we create anew_headthat points to it to facilitate the swapping process.For the example list1 -> 2 ... 阅读全文
posted @ 2015-07-06 14:22 jianchao-li 阅读(181) 评论(0) 推荐(0) 编辑
摘要:动态规划: 1 class Solution { 2 public: 3 /** 4 * @param s: A string 5 * @param p: A string includes "?" and "*" 6 * @return: A boolean... 阅读全文
posted @ 2015-07-06 12:06 jianchao-li 阅读(222) 评论(0) 推荐(0) 编辑
摘要:Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see aC++DP solution that gets accepted, please l... 阅读全文
posted @ 2015-07-06 11:57 jianchao-li 阅读(242) 评论(0) 推荐(0) 编辑
摘要:Well, the idea of this problem is actually very sample --- keep merging the unmerged lists in lists until there is exactly one list remained. However,... 阅读全文
posted @ 2015-07-06 00:49 jianchao-li 阅读(166) 评论(0) 推荐(0) 编辑
摘要:1 class Solution { 2 public: 3 /** 4 * @param s: A string 5 * @param p: A string includes "." and "*" 6 * @return: A boolean 7 ... 阅读全文
posted @ 2015-07-05 23:53 jianchao-li 阅读(296) 评论(0) 推荐(0) 编辑
摘要:This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[0..i) matches p[0..j) and false otherwise. ... 阅读全文
posted @ 2015-07-05 23:40 jianchao-li 阅读(409) 评论(0) 推荐(0) 编辑
摘要:To be honest, I do not know whether this problem is designed to let you use stacks. Anyway, I don't. Here are my codes, both BFS and DFS version. 1 ... 阅读全文
posted @ 2015-07-04 10:57 jianchao-li 阅读(229) 评论(0) 推荐(0) 编辑
摘要:1 class Solution { 2 public: 3 /** 4 * @param A: An integer array. 5 * @param B: An integer array. 6 * @return: a double whose for... 阅读全文
posted @ 2015-07-03 23:33 jianchao-li 阅读(334) 评论(0) 推荐(0) 编辑
摘要:This link has a very concise and fast solution based on binary search. Spend some time reading it and make sure you understand it. It is very helpful.... 阅读全文
posted @ 2015-07-03 23:20 jianchao-li 阅读(211) 评论(0) 推荐(0) 编辑
摘要:This is a tough problem! I read some solutions from the web. This link provides a one-end BFS solution. The code is reorganized as follows. 1 class So... 阅读全文
posted @ 2015-07-03 16:05 jianchao-li 阅读(343) 评论(0) 推荐(0) 编辑
摘要:This is a classic problem of Dynamic Programming. We define the statedp[i][j]to be the minimum number of operations to convertword1[0..i - 1]toword2[0... 阅读全文
posted @ 2015-07-02 22:12 jianchao-li 阅读(201) 评论(0) 推荐(0) 编辑
摘要:The idea to solve this problem is to first sort the intervals according to theirstartfield and then scan the intervals from head to tail and merge tho... 阅读全文
posted @ 2015-07-02 18:43 jianchao-li 阅读(253) 评论(0) 推荐(0) 编辑
摘要:A classic subroutine of merge sort. Just merge the elements from back to forth. Keep a pointer for the merged position of the element and two other po... 阅读全文
posted @ 2015-07-02 18:11 jianchao-li 阅读(177) 评论(0) 推荐(0) 编辑
摘要:The typical solution to this problem is to use Dynamic Programming. The state dp[i] represents whether s[0..i - 1] can be broken into words in wordDic... 阅读全文
posted @ 2015-07-02 11:48 jianchao-li 阅读(268) 评论(0) 推荐(0) 编辑
摘要:This link suggests a concise C++ recursive solution. The original code may be hard to understand at first and I have rewritten the code below. You may... 阅读全文
posted @ 2015-07-02 11:35 jianchao-li 阅读(520) 评论(0) 推荐(0) 编辑
摘要:If you have solved the N-Queens problem, this one can be solved in a similar manner. Starting from the first row, we try each of its columns. If there... 阅读全文
posted @ 2015-07-01 22:28 jianchao-li 阅读(223) 评论(0) 推荐(0) 编辑
摘要:The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.The idea is also very simple. Sta... 阅读全文
posted @ 2015-07-01 21:16 jianchao-li 阅读(252) 评论(0) 推荐(0) 编辑
摘要:1 class Solution { 2 public: 3 /* 4 * @param n: An integer 5 * @return: True or false 6 */ 7 bool checkPowerOf2(int n) { 8 ... 阅读全文
posted @ 2015-07-01 01:59 jianchao-li 阅读(423) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示