随笔分类 - LeetCode Solutions
Solutions to problems on https://leetcode.com/problemset/algorithms/.
摘要:I think the following code is self-explanatory enough. We use anunordered_map countsto record the expected times of each word and anotherunordered_map...
阅读全文
摘要: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)). One of the simplest ideas is to comp...
阅读全文
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ...
阅读全文
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ...
阅读全文
摘要:Well, since theheadpointer may also been modified, we create anew_headthat points to it to facilitate the swapping process.For the example list1 -> 2 ...
阅读全文
摘要:Well, so many people has tried to solve this problem using DP. And almost all of them get TLE (if you see aC++DP solution that gets accepted, please l...
阅读全文
摘要:Well, the idea of this problem is actually very sample --- keep merging the unmerged lists in lists until there is exactly one list remained. However,...
阅读全文
摘要:This problem has a typical solution using Dynamic Programming. We define the state P[i][j] to be true if s[0..i) matches p[0..j) and false otherwise. ...
阅读全文
摘要: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 ...
阅读全文
摘要:This link has a very concise and fast solution based on binary search. Spend some time reading it and make sure you understand it. It is very helpful....
阅读全文
摘要:This is a tough problem! I read some solutions from the web. This link provides a one-end BFS solution. The code is reorganized as follows. 1 class So...
阅读全文
摘要:This is a classic problem of Dynamic Programming. We define the statedp[i][j]to be the minimum number of operations to convertword1[0..i - 1]toword2[0...
阅读全文
摘要:The idea to solve this problem is to first sort the intervals according to theirstartfield and then scan the intervals from head to tail and merge tho...
阅读全文
摘要: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..i - 1] can be broken into words in wordDic...
阅读全文
摘要: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. You may...
阅读全文
摘要: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. If there...
阅读全文
摘要:The idea is to use backtracking. In fact, the code below uses DFS, which involves backtracking in a recursive manner.The idea is also very simple. Sta...
阅读全文
摘要: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. This can be pro...
阅读全文
摘要:Well, if you have got this problem accepted, you may have noticed that there are 7 suggested solutions for this problem. The following passage will im...
阅读全文

浙公网安备 33010602011771号