热爱人工智能、机器学习和算法;截至2018年1月2日,LeetCode用户Most Reputation排名第2(https://discuss.leetcode.com/users?section=sort-reputation)
我的博客即将入驻“云栖社区”,诚邀技术同仁一同入驻。 Hi, Jianchao's Blog is gonna to be moved to the Yunqi Community of Alibaba Cloud. Welcome to join with me :-)
The code is as follows. public class Solution { public int getSum(int a, int b) { return b == 0 ? a : getSum(a ^ b, (a & b)
A very typical application of hash maps. Since I am now learning Java, I code in Java. The following code uses toCharArray() and getOrDefault(), which are learnt from this post.
Problem Description: Given two sparse matrices A and B, return the result of AB. You may assume that A's column number is equal to B's row number.
This problem requires a new data structure --- Segment Tree. You may use this GeeksforGeeks article to get some ideas of it.
Af first I read the title as "Addictive Number". Anyway, this problem can be solved elegantly using recursion.
Problem Description: A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand operation which turns the water at position (row, col) into a land.
Very similar to Range Sum Query - Immutable, but we now need to compute a 2d accunulated-sum. In fact, if you work in computer vision, you may know a name for such an array --- Integral Image.
The idea is fairly straightforward: create an array accu that stores the accumulated sum fornums such that accu[i] = nums[0] + .
This post shares very detailed explanation of how to use binary search to find the boundaries of the smallest rectangle that encloses the black pixels.
This problem can be solved very elegantly using BFS, as in this post. The code is rewritten below in C++.
A typical O(n^2) solution uses dynamic programming. Let's use lens[j] to denote the length of the LIS ending with nums[j].
This problem seems to be easy at the first glance, especially the problem gives a too much simpler example.
Problem Description: Given a binary tree, find the length of the longest consecutive sequence path. The path refers to any sequence of nodes from so...
I adopt a way similar to that of the OJ to serialize the binary tree. For the following tree, my serialization result is "1,2,3,null,null,4,5," (note the last comma).
Problem Description: A group of two or more people wants to meet and minimize the total travel distance.
This post shares a very nice solution using two heaps: a max heap for the smaller half and a min heap for the larger half.
Problem Description: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you a...
Problem Description: You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you a...
Haha, an interesting problem. Just try to let your opponent start with a number that is an integer multiple of 4.
Problem Description: Given a pattern and a string str, find if str follows the same pattern. Here follow means a full match, such that there is a bi...
This problem can be solved in very neat codes (yeah, you will see Stefan posting many nice solutions).
A solution using additional spaces to solve a copy of the original board is easy. To get an in-place solution, we need to record the information of state transitions directly in the board.
Problem Description: An abbreviation of a word follows the form . Below are some examples of word abbreviations: a) it --> it ...
There are mainly two solutions to solve this problem. The first one is very clever, using the idea of cycle detection, and runs in O(n) time.
Problem Description: You are given a m x n 2D grid initialized with these three possible values. -1 - A wall or an obstacle.
Problem Description: Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
An interesting problem about iterators. This post shares a very nice solution, which is rewritten below, with minor simplifications.
This post shares a very nice solution, which is rewritten below. 1 class Solution { 2 public: 3 vector addOperators(string num, int target) { 4 int n = num.
The key challenge to this problem is to make the code clean. This post has shared a nice example, which is rewritten below in C++.
Problem Description: Given two 1d vectors, implement an iterator to return their elements alternately.
Problem Description: Given an unsorted array nums, reorder it in-place such that nums[0] = nums[2] = nums[i - 1]; If i is even, then nums[i] nums[i...
Well, after seeing the similar problems, you may have known how to solve this problem. Yeah, just to construct larger numbers from smaller numbers by adding perfect squares to them.
This post shares a nice explanation, which is implemented here. The code is rewritten below. 1 class Solution { 2 public: 3 string minWindow(string s, string t) { 4 int m = s.
This problem is relatively easy. The challenge is how to get a concise code. This post shares a very elegant one which traverses each cell of board only once.
Just don't be scared by this problem :-) It's also very standard backtracking problem. This post shares a very concise code, which is rewritten below in C++.
Just use binary search to find the first bad version. The code is as follows. 1 // Forward declaration of isBadVersion API.
Problem Description: Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist one celebrity.
Problem Description: There is a fence with n posts, each post can be painted with one of the k colors.
This problem is designed specifically to use binary search. In fact, in H-Index, someone has already used this idea (you may refer to this post :-)) The code is as follows.
If you've read the Wikipedia article of H-Index, there is already a neat formula there for computing the h-index, which is written below using the notations of the problem.
This link shares a nice solution with explanation using DP. You will be clear of the algorithm after running it on its suggested example: matrix = ...
The basic idea is as follows: Create a new_head that points to head and use it to locate the immediate node before them-th (notice that it is 1-inde...
This link has a very neat code, which is rewritten below using stack since the push and pop operations of it are O(1) time, while the pop_back and push_back of vector tend to be more time-consuming.
This problem is not difficult. But it is not easy to have a bug-free code. As you write your codes according to the hints, the most important thing is...
Problem Description: Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.
Problem Description: Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of strings.
1 /** 2 * class VersionControl { 3 * public: 4 * static bool isBadVersion(int k); 5 * } 6 * you can use VersionControl::isBa...
An interesting problem. The code is also short and clear. The basic idea is to use a 2d array dp[i][j] to denote the minimum hp that is required before entering dungeon[i][j].
Problem Description: Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.