随笔分类 -  LeetCode Solutions

上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页
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... 阅读全文
posted @ 2015-07-06 22:32 jianchao-li 阅读(222) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-06 18:16 jianchao-li 阅读(233) 评论(0) 推荐(0)
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ... 阅读全文
posted @ 2015-07-06 15:08 jianchao-li 阅读(173) 评论(0) 推荐(0)
摘要:Well, since theheadpointer may also be modified, we create anew_headthat points to it to facilitate the reverse process.For the example list1 -> 2 -> ... 阅读全文
posted @ 2015-07-06 14:54 jianchao-li 阅读(259) 评论(0) 推荐(0)
摘要:Well, since theheadpointer may also been modified, we create anew_headthat points to it to facilitate the swapping process.For the example list1 -> 2 ... 阅读全文
posted @ 2015-07-06 14:22 jianchao-li 阅读(186) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-06 11:57 jianchao-li 阅读(245) 评论(0) 推荐(0)
摘要: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,... 阅读全文
posted @ 2015-07-06 00:49 jianchao-li 阅读(168) 评论(0) 推荐(0)
摘要: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. ... 阅读全文
posted @ 2015-07-05 23:40 jianchao-li 阅读(414) 评论(0) 推荐(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 ... 阅读全文
posted @ 2015-07-04 10:57 jianchao-li 阅读(233) 评论(0) 推荐(0)
摘要: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.... 阅读全文
posted @ 2015-07-03 23:20 jianchao-li 阅读(217) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-03 16:05 jianchao-li 阅读(348) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-02 22:12 jianchao-li 阅读(204) 评论(0) 推荐(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... 阅读全文
posted @ 2015-07-02 18:43 jianchao-li 阅读(259) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-02 18:11 jianchao-li 阅读(186) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-02 11:48 jianchao-li 阅读(274) 评论(0) 推荐(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. You may... 阅读全文
posted @ 2015-07-02 11:35 jianchao-li 阅读(527) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-01 22:28 jianchao-li 阅读(228) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-07-01 21:16 jianchao-li 阅读(257) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-06-30 11:54 jianchao-li 阅读(488) 评论(0) 推荐(0)
摘要: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... 阅读全文
posted @ 2015-06-30 11:32 jianchao-li 阅读(294) 评论(0) 推荐(0)

上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页