热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
Again, a classic interview question of linked list. Similar to Linked List Cycle, we maintain two pointers fast and slow: fast move two steps at one time while slow moves one step at a time.
A classic interview question of linked list. The idea is to maintain two pointers, one move one step at a time and the other move two steps at a time.
This problem has a long story. There are just too many solutions on the web and it can be studied for a long time before you fully grasp it.
Well, this problem looks easy at first glance. However, to get a bug-free code may be not that easy. The total square is simply equal to the sum of t...
Well, there many ways to solve this problem. Let's first look at a naive solution. The basic idea is simple.
The key of this problem is that we need not build the tree from scratch. In fact, we can direct obtain its post-order traversal results in a recursive...
This is a application of the Trie data structure, with minor extension. The critical part in this problem is to count all the words that have a partic...
A direct applicatin of the heap data structure. Specifically, a max heap is used. The required functions include insertion of a node to the heap and extraction of the maximum element of the heap.
The idea is to use two stacks. For push, the first stack records the pushed elements and the second stack records all the minimum (including duplicates) that have been seen.
This problem is an application of the Trie data structure. In the following, it is assumed that you have solved Implement Trie (Prefix Tree).
Well, the problem does not aim for an advanced algorithm like KMP but only a clean brute-force algorithm.
In this problem, we are asked to divide two integers. However, we are not allowed to use division, multiplication and mod operations.
To read n characters, we first call read4 for n / 4 times. For example, if we want to read 10 characters, we will read them in the 8 (4 * 2) + 2 manne...
Problem Description: Design and implement a TwoSum class. It should support the following operations: add and find.
Recently I systematicall review some sorting algorithms, including insertion sort, bubble sort, merge sort and quick sort.
Recently I reviewed the classic heapsort algorithm and implement it according to contents in Introduction to Algorithms (3rd edition).
The problem has a nice structure that backtracking naturally fits in. The structure is, given a starting position idx, we search from idx till the end of the string s.
Well, this problem desires for the use of dynamic programming. They key to any DP problem is to come up with the state equation.
Recursive (Backtracking) This is a typical problem that can be tackled by backtracking. Since backtracking has a more-or-less similar template, so I do not give explanations for this method.
This problem is similar to Best Time to Buy and Sell Stock. Given prices, we find the day (denoted as buy) of the first local minimum and the day (den...
This post shares a very simple solution, whose code is rewritten below, just 5 lines :-) 1 class Solution { 2 public: 3 int maxProfit(vect...
This problem seems to be tricky at first glance. However, if you know Morris traversal, it is just the preorder case of Morris traversal and the code is really short.
There are many merge-sort solutions at the forum, but very few quicksort solutions. So I post my accepted quicksort solution here.
This is a typical DP problem. Suppose the minimum path sum of arriving at point (i, j) is S[i][j], then the state equation is S[i][j] = min(S[i - 1][j], S[i][j - 1]) + grid[i][j].
Well, this problem is similar to Unique Paths. The introduction of obstacles only changes the boundary conditions and make some points unreachable (simply set to 0).
This is a fundamental DP problem. First of all, let's make some observations. Since the robot can only move right and down, when it arrives at a poin...
Well, this problem has a naive solution, which is to sort the array in descending order and return the k-1-th element.
Well, I do not see what this problem is for. The same code of Binary Tree Level Order Traversal can be used here.
A classic tree traversal problem. I share my two solutions here: BFS and DFS. BFS: 1 vector levelOrder(TreeNode *root) { 2 vector l...
This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...
This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...
This is a fundamental and yet classic problem. I share my three solutions here: Iterative solution using stack --- O(n) time and O(n) space; Recurs...
Well, the basic idea is fairly straightforward. We maintain a mapping mp from a value in nums to its position (index) i.
Well, have you solved the nextPermutation problem? If so and you have handled the cases of duplicates at that problem, your code can be used in this problem.
Well, have you solved the nextPermutation problem? If so, your code can be used in this problem. The idea is fairly simple: sort nums in ascending o...
Well, in fact the problem of next permutation has been studied long ago. From the Wikipedia page, in the 14th century, a man named Narayana Pandita gi...