热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
I think the following code is self-explanatory enough. We use an unordered_map counts to record the expected times of each word and another unordered_map seento record the times we have seen.
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)).
Well, since the head pointer may also be modified, we create a new_head that points to it to facilitate the reverse process.
Well, since the head pointer may also be modified, we create a new_head that points to it to facilitate the reverse process.
Well, since the head pointer may also been modified, we create a new_head that points to it to facilitate the swapping process.
动态规划: 1 class Solution { 2 public: 3 /** 4 * @param s: A string 5 * @param p: A string includes "?" and "*" 6 * @ret...
Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see a C++ DP solution that gets accepted, please let me know ^_^).
Well, the idea of this problem is actually very sample --- keep merging the unmerged lists in lists until there is exactly one list remained.
1 class Solution { 2 public: 3 /** 4 * @param s: A string 5 * @param p: A string includes ".
This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[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 class Solution { 2 public: 3 /** 4 * @param A: An integer array. 5 * @param B: An integer array.
This link has a very concise and fast solution based on binary search. Spend some time reading it and make sure you understand it.
This is a tough problem! I read some solutions from the web. This link provides a one-end BFS solution.
This is a classic problem of Dynamic Programming. We define the state dp[i][j] to be the minimum number of operations to convert word1[0.
The idea to solve this problem is to first sort the intervals according to their start field and then scan the intervals from head to tail and merge those that overlap.
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...
The typical solution to this problem is to use Dynamic Programming. The state dp[i] represents whether s[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.
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.
The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.
1 class Solution { 2 public: 3 /* 4 * @param n: An integer 5 * @return: True or false 6 */ 7 bool checkPowerOf2(...
Each time we find a match, increase the global counter by 1. For KMP, algorithm, you may refer to the following links which have nice explanations.
Well, this problem becomes a little trickier since there may be more than one majority element. But, there can be at most two of them.
Well, if you have got this problem accepted, you may have noticed that there are 7 suggested solutions for this problem.
1 class Solution { 2 public: 3 /** 4 * @param dictionary: a vector of strings 5 * @return: a vector of strings 6 */ ...
1 class Solution { 2 public: 3 /** 4 * @param string: An array of Char 5 * @param length: The true length of the string 6 ...
暴力解法(O(mn)): 1 class Solution { 2 public: 3 /** 4 * Returns a index to the first occurrence of target in source, 5 * or -1 if target is not part of source.
1 /** 2 * Definition of ListNode 3 * class ListNode { 4 * public: 5 * int val; 6 * ListNode *next; 7 * ListNode(int v...
Well, life gets difficult pretty soon whenever the same operation on array is transferred to linked list.
1 class Solution { 2 public: 3 /** 4 * @param A: sorted integer array A which has m elements, 5 * but size of A is...
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and O(n) space): 1 /** 2 * Definition of TreeN...
A subroutine of merge sort. 1 class Solution { 2 public: 3 /** 4 * @param A and B: sorted integer array A and B.
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and O(n) space): 1 /** 2 * Definition of TreeN...
The recursive solution is trivial and I omit it here. Iterative Solution using Stack (O(n) time and O(n) space): 1 /** 2 * Definition of TreeN...
动态规划: lis[i] = max_{j = 0, 1, ..., i - 1, nums[j] < nums[i]} lis[j] + 1 1 class Solution { 2 public: 3 /** 4 * @param nums: The in...
1 class Solution { 2 public: 3 /** 4 * @param strs: A list of strings 5 * @return: The longest common prefix 6 */ ...
Bit-by-Bit summation: 1 class Solution { 2 public: 3 /* 4 * @param a: The first integer 5 * @param b: The second integer 6...
The hints of the problem has given detailed information to implement the topological sorting algorithm.
BFS: 1 /** 2 * Definition for Directed graph. 3 * struct DirectedGraphNode { 4 * int label; 5 * vector neighbors; 6 * D...
基于快速排序: 1 class Solution { 2 public: 3 /* 4 * param k : description of k 5 * param nums : description of array and index 0 ~...
1 class Solution { 2 public: 3 /** 4 * @param grid: a list of lists of integers. 5 * @return: An integer, minimizes the sum o...
Well, to compute the number of trailing zeros, we need to first think clear about what will generate a trailing 0? Obviously, a number multiplied by 10 will have a trailing 0 added to it.
1 class Solution { 2 public: 3 // param n : description of n 4 // return: description of return 5 long long trailingZeros(lon...
Well, the basic idea is very simple. Start from the tail of s and move backwards to find the first non-space character.
1 class Solution { 2 public: 3 /** 4 * @param s A string 5 * @return the length of last word 6 */ 7 int lengthOf...
1 class Solution { 2 public: 3 /** 4 * @param s A string 5 * @return Whether the string is a valid palindrome 6 */ 7...
1 class Solution { 2 public: 3 /** 4 * @param s A string 5 * @return whether the string is a valid parentheses 6 */ ...
递归实现: 1 /** 2 * Definition of TreeNode: 3 * class TreeNode { 4 * public: 5 * int val; 6 * TreeNode *left, *right; 7 * ...
1 class MinStack { 2 public: 3 MinStack() { 4 // do initialization if necessary 5 } 6 7 void push(int number) { ...