热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
Personally, this is a relatively difficult DP problem. This link posts a typical DP solution to it. You may need some time to get how it works.
The function signature has been updated to return a more intuitive vectorwhich treats a single string as a group of anagrams consisting of only itself.
Hash Table This idea uses a hash table to record the times of appearances of each letter in the two stringss and t.
To solve this problem, you first need to understand it well. The key problem is tell the difference of scramble from permutations.
Well, this problem seems to be a little tricky at first glance. However, the idea (taken from this link) is really simple.
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 their neighbors, not all those with lower ratings.
Well, a dynamic programming problem. Let's first define its state dp[i][j] to be the number of distinct subsequences of t[0.
Well, the idea is to search from the top-right element and then reduce the range for further searching by comparisons between target and the current element.
This problem is more or less the same as Find Minimum in Rotated Sorted Array. And one key difference is as stated in the solution tag.
As explained in the Solution tag, the key to solving this problem is to use invariants. We set two pointers: l for the left and r for the right.
For those who have already solved Search in Rotated Sorted Array, this problem can be solved similarly using codes for that problem and simply adding codes to skip the duplicates.
This problem is a nice application of binary search. The key lies in how to determine the correct half for target.
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...
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 show some of them in the following.
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]].
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...
This problem has a nice BFS structure. Let's illustrate it using the example nums = [2, 3, 1, 1, 4] in the problem statement.
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& nums) { 4 int i = 0, n = nums.
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 node to it.
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 matrices.
Note: The following idea is in fact from the last answer in this link, which leads to a clean code. I just reorganize it and add some explanations.
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 ...
Well, since we need to make a deep copy of the list and nodes in the list have a random pointer that may point to any node in the list (or NULL), we n...
Problem Description Given a string, find the length of the longest substring T that contains at most 2 distinct characters.
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.
Well, a follow-up for the problem Lowest Common Ancestor of a Binary Search Tree. However, this time you cannot figure out which subtree the given nodes lie in according to their values.
Use the strs[0] as the reference string and then compare it with the remaining strings from left to right.
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 ...
1 class Solution { 2 public: 3 /** 4 * @param nums: a vector of integers 5 * @return: an integer 6 */ 7 int ...
1 /** 2 * Definition for a point. 3 * struct Point { 4 * int x; 5 * int y; 6 * Point() : x(0), y(0) {} 7 * Point(...
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 problem becomes more difficult once the binary tree is not perfect. The idea is still similar to use a level-order traversal.
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.
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, remember to take advantage of the property of binary search trees, which is, node -> left -> val < node -> val < node -> right -> val.
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 clean code.
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 following idea is taken from a book named 《剑指offer》 published in China. Suppose n = 271, it then breaks [1, 271] into [1, 71] and [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 required to be removed.
1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int v...
1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int v...
1 class Solution { 2 public: 3 /** 4 * @param A: a vector of integers 5 * @return: an integer 6 */ 7 int fir...
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.
递归实现: 1 class Solution { 2 public: 3 /** 4 * @param nums: A list of integers. 5 * @return: A list of unique permutations.
递归实现: class Solution { public: /** * @param nums: A list of integers. * @return: A list of permutations.