热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
Problem Description: There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you.
This link has two nice solutions, one updating from forth to back (posted by tqlong in the post) and the other updating from back to forth (posted by diego2 in the answer).
There are three methods to solve this problem: bit manipulation, rearrangement of the array, and math tricks.
Problem Description: Given a string s, return all the palindromic permutations (without duplicates) of it.
Problem Description: Given a string, determine if a permutation of the string could form a palindrome.
The idea and code is just taken from this link. There is a nice explanation to the code on the answer by BrianLuong1337.
Stefan is a hero! Personally, I love his first C++ solution. 1 class Solution { 2 public: 3 int nthUglyNumber(int n) { 4 vector ...
No matter what language you use on the OJ, you may refer to Stefan's post for a solution. The C++ is rewritten below, just really concise.
The idea is very simple: just extract the numbers from each version number and compare the numbers from beginning to the end.
The basic idea is to store the key-value pair in some container. In the following code, we make three type definitions: 1 typedef list LI; 2 typed...
The idea is similar to Strobogrammatic Number II: generate all those in-range strobogrammatic numbers and count.
Problem Description: Given n nodes labeled from 0 to n - 1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree.
A typical DFS problem. Just go ahead... 1 class Solution { 2 public: 3 bool exist(vector& board, string word) { 4 int m = board.
A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-) The following code is ...
You need to understand what a Trie is before writing the code :-) This link has a nice solution, whose code is rewritten below.
This problem is purely to test your coding ability. Just go ahead :-) 1 class Solution { 2 public: 3 string countAndSay(int n) { 4 ...
Try to split all the numbers into two groups with each of the target one in different groups. Refer to this link for a nice explanation.
Problem Description: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0
Well, no matter whether the number is happy or not, its sum-of-squared-digits sequance has a cycle. Well, do you still remember the algorithm for dete...
Dynamic Programming There is a nice introduction to the DP algorithm in this Wikipedia article. The idea is to maintain a running maximum smax and a current summation sum.
Well, if you have no idea of other methods, try to compue the result for all the numbers ranging from 1 to 20 and then you will see the regularity.
The basic idea is to start from root and add it to the current path, then we recursively visit its left and right subtrees if they exist; otherwise, w...
The idea is to add the two binary numbers (represented as strings) from back to forth bit by bit and store the result in the longer string.
This problem is just similar to Minimum Depth of Binary Tree. The first solution also uses recursion (not sure whether it can be called DFS).
Well, this problem has the highest acceptance rate among all OJ problems. It has a very easy 1-line reursive solution.
The basic idea is to use binary search: keep two pointers l and r for the current search range, then find the middle element nums[mid] in this range.
The idea is to search for the left and right boundaries of target via two binary searches. Well, some tricks may be needed.
The most obvious idea is to maintain two divisors to get the most and least significant digits and compare them.
The idea is to add the two numbers (represented by linked lists) nodes after nodes and store the result in the longer list.
The basic idea is to maintain a hash table for each element num in nums, using num as key and its index (1-based) as value.
This problem can be solved easily if we are allowed to use more than O(1) space. For example, you may create a copy of the original matrix (O(mn)-spac...
This problem is a little tricky at first glance. However, if you have finished the House Robberproblem, this problem can simply be decomposed into two House Robber problems.
Since we are not allowed to rob two adjacent houses, we keep two variables pre and cur. During the i-th loop, pre records the maximum profit that we d...
Well, this problem can be solved in 1-line clearly. Take a look at this link :-) 1 class Solution { 2 public: 3 string convertToTitle(int n) ...
Problem Description: There are a row of n houses, each house can be painted with one of the three colors: red, blue or green.
This problem can be solved by DP elegantly. You may refer to this link for the code and explanations.
The idea is just to perform the addition from right to left as usual :-) Note that the result may be longer than the original digits by 1 number (the carry).
The idea is just to add the elements in the spiral order. First the up-most row (u), then the right-most column (r), then the down-most row (d), and finally the left-most column (l).
The idea is just to generate the matrix in the spiral order. First the up-most row (u), then the right-most column (r), then the down-most row (d), and finally the left-most column (l).
A simple and in-place idea: first reverse the image in row-major order and then transpose it :-) 1 class Solution { 2 public: 3 void rotat...
This problem has a naive solution using sort and linear scan. The suggested solution uses the idea of bucket sort.
This problem can be solved easily once you find the regularities :-) This link has done it for you. You may refer to its Python version.
Problem Description: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.
Problem Description: Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],.
Problem Description: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).
Problem Description: This is a follow up of Shortest Word Distance. The only difference is now you are given the list of words and your method will b...
Problem Description: Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [1,2], [3], [4,5,6] ] By callin...
A relatively difficult tree problem. Well, a recursive solution still gives clean codes. The tricky part of this problem is how to record the result.
An extension of Best Time to Buy and Sell Stock III. The idea is still to use dynamic programming (see here for detailed introduction).