09 2015 档案
摘要: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. You may r...
阅读全文
摘要:Problem Description:You are given am x n2D grid initialized with these three possible values.-1- A wall or an obstacle.0- A gate.INF- Infinity means a...
阅读全文
摘要:Problem Description:Given a binary search tree and a node in it, find the in-order successor of that node in the BST.Note: If the given node has no in...
阅读全文
摘要:An interesting problem about iterators. This post shares a very nice solution, which is rewritten below, with minor simplifications.// Below is the in...
阅读全文
摘要:This post shares a very nice solution, which is rewritten below. 1 class Solution { 2 public: 3 vector addOperators(string num, int target) { 4 ...
阅读全文
摘要: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++.class Solution {publ...
阅读全文
摘要:Problem Description:Given two 1d vectors, implement an iterator to return their elements alternately.For example, given two 1d vectors:v1 = [1, 2]v2 =...
阅读全文
摘要:Problem Description:Given an unsorted arraynums, reorder itin-placesuch thatnums[0] = nums[2] = nums[i - 1];Ifiis even, thennums[i] & nums) {4 ...
阅读全文
摘要: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 ...
阅读全文
摘要:This post shares a nice explanation, which is implemented here. The code is rewritten below. 1 class Solution { 2 public: 3 string minWindow(strin...
阅读全文
摘要: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 on...
阅读全文
摘要: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 ...
阅读全文
摘要:Just use binary search to find the first bad version.The code is as follows. 1 // Forward declaration of isBadVersion API. 2 bool isBadVersion(int ver...
阅读全文
摘要: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. The definition
阅读全文
摘要:Problem Description:There is a fence with n posts, each post can be painted with one of the k colors.You have to paint all the posts such that no more...
阅读全文
摘要: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 :-))Th...
阅读全文
摘要: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 not...
阅读全文
摘要: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 = [[0,...
阅读全文
摘要:The basic idea is as follows:Create anew_headthat points toheadand use it to locate the immediate node before them-th (notice that it is1-indexed) nod...
阅读全文
摘要:This link has a very neat code, which is rewritten below using stacksince thepushandpopoperations of it areO(1)time, while thepop_backandpush_backofve...
阅读全文