热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
1 class Queue { 2 public: 3 stack stack1; 4 stack stack2; 5 6 Queue() { 7 // do intialization if necessary 8 ...
1 class Solution { 2 public: 3 /** 4 * @param A, B: Two strings. 5 * @return: The length of longest common subsequence of A and B.
1 class Solution { 2 public: 3 /** 4 * @param A, B: Two string. 5 * @return: the length of the longest common substring.
Well, this problem is spiritually similar to to Course Schedule. You only need to store the nodes in the order you visit into a vector during BFS or DFS.
As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by prerequisites.
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge (u, v) from node u to node v in the graph, u must appear before v in the topological sort.
Graph is an important data structure and has many important applications. Moreover, grach traversal is key to many graph algorithms.
Problem Description: Given two strings S and T, determine if they are both one edit distance apart.
This problem is similar to Missing Ranges and easier than that one. The idea is to use two pointers to find the beginning and end of a range and then push it into the result.
Problem Description: Given a sorted integer array where the range of elements are [lower, upper] inclusive, return its missing ranges.
Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur.
The problem statement has stated that there are both O(n) and O(nlogn) solutions to this problem. Let's see the O(n) solution first (taken from this link), which is pretty clever and short.
Problem Description: Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.
This link has a great discussion about this problem. You may refer to it if you like. In fact, the idea and code in this passage is from the former link.
Well, this problem has a nice BFS structure. Let's see the example in the problem statement. start = "hit" end = "cog" dict = ["hot", "dot", "dog"...
This problem is not quite difficult (a typical BFS traversal of graph), though, its aceptance rate is relatively low.
This problem is an application of graph traversal, which has two systematic methods: Bread-First Search (BFS) and Depth-First Search (DFS).
To solve this problem, some observations have to be made first. Let's first see two relatively easy observations.
This is a typical problem about searching. In fact, you can use either BFS or DFS for it. Personally, I use BFS because I think it is more intuitive and easy to implement.
The basic idea of is as follows: Maintain a deque operands for the numbers and another deque operations for the operators +, -, *,/`.
This problem can be solved elegantly using dynamic programming. We maintain two arrays: cnt[i][j] --- number of parentheses needed to add within s[i.
KMP is a classic and yet notoriously hard-to-understand algorithm. However, I think the following two links give nice explanations.
Well, a typical backtracking problem. Make sure you are clear with the following three problems: What is a partial solution and when is it finished?...
Well, a typical backtracking problem. Make sure you are clear with the following three problems: What is a partial solution and when is it finished?...
A typical backtracking problem. For any backtracking problem, you need to be think about three ascepts: What is a partial solution and when is it fi...
The Longest Increasing Subsequence (LIS) problem requires us to find a subsequence t of a given sequence s, such that t satisfies two requirements: ...
This is the classic LCS problem. Since it only requires you to print the maximum length, the code can be optimized to use only O(m) space (see here).
This is the classic LCS problem. Since it requires you to print one longest common subsequence, just use the O(m*n)-space version here.
The Longest Common Subsequence (LCS) problem is as follows: Given two sequences s and t, find the length of the longest sequence r, which is a subsequence of both s and t.
The Longest Common Substring (LCS) problem is as follows: Given two strings s and t, find the length of the longest string r, which is a substring of both s and t.
This problem is a nice extension of the Valid Parentheses problem. There are several ways to solve it.
Well, there are two ways to add a open or close parenthesis to the current string. If number of ( is less than n, you can add (; If number of ) is less than number of (, you can add ).
This is a classic problem of the application of stacks. The idea is, each time we meet a (, { or[, we push it to a stack.
Problem Description: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node...
After reading the quote below the problem, you will be motivated to solve it immediately :-) Well, indeed it is also relative easy.
This problem is not very intuitive at first glance. However, the final idea should be very self-explanatory.
The idea is to find the longest palindromic substring of s that begins with s[0]. Then take the remaining susbtring, reverse it and append it to the beginning of s.
Well, this problem has a O(n^3) solution similar to 3Sum. That is, fix two elements nums[i] and nums[j] (i < j) and search in the remaining array for ...
This problem is very similar to 3Sum. You only need to maintain a variable for the sum that is closet to target.
This is an extension of the 2Sum problem. The idea is pretty simple (even no need to use hash). Sort the array and then starting from the first elemen...
Well, it seems that many people meet the TLE problem. Well, I use a simple trick in my code to aoivd TLE.
Well, an extension of Remove Duplicates from Sorted Array. The program is fairly similar to that in this solution.
Well, a medium problem. Use two pointers. One points to the curren number and the other points to the last unique number.
Radix sort is another linear time sorting algorithm. It sorts (using another sorting subroutine) the numbers from their least significant digits to most significant digits.
Well, this problem is designed for radix sort. For more information about radix sort, Introduction to Algorithms, 3rd edition has some nice examples.
Counting sort is a linear time sorting algorithm. It is used when all the numbers fall in a fixed range.
The hints on below the problem have suggested a two-pass solution. Now I will focus on the one-pass solution.
This problem gets much trickier than Contains Duplicate and Contains Duplicate II. The basic idea is to maintain a window of k numbers.
A classic problem of hash set. The unordered_set of C++ is very suitable for this problem. The code is as follows and it should be quite self-explanatory.
Problem Description: Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.