leetcode
文章平均质量分 63
圆形毕露
技术热爱者
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法
归并排序可以有两种思路----top-down 和 bottom-uptop-down:递归实现,将数组分成两半,分别处理;再合并。伪代码如下:split ( A[], l, r){ if ( r - l < 2) return; m = (r + l) / 2; split ( A, l, m); //split A[l…m-1] split ( A, m, r); /原创 2014-10-08 20:38:27 · 1093 阅读 · 0 评论 -
Unique Binary Search Trees II [leetcode]
vector generateTrees(int n) { return getTree(1, n); } vector getTree(int start, int end) { vector curRes; if (start > end) { curRes.push_b原创 2014-09-27 23:40:22 · 521 阅读 · 0 评论 -
Binary Tree Inorder Traversal [leetcode] 非递归的三种解法
第一种方法是Morris Traversal是O(n)时间复杂度,且不需要额外空间的方法。缺点是需要修改树。通过将叶子节点的right指向其中序后继。代码如下 vector inorderTraversal(TreeNode *root) { vector res; TreeNode * cur = root; TreeNode原创 2014-09-27 20:46:21 · 973 阅读 · 0 评论 -
Gray Code [leetcode]
第一种方法是直接排列以二进制为0值的格雷码为第零项,第一项改变最右边的位元,第二项改变右起第一个为1的位元的左边位元,第三、四项方法同第一、二项,如此反复,即可排列出n个位元的格雷码。 vector grayCode(int n) { vector res; res.push_back(0); if (n == 0) return原创 2014-09-27 18:56:58 · 687 阅读 · 0 评论 -
Generate Parentheses [leetcode] 循环,DP和递归
递归 vector generateParenthesis(int n) { vector res; generate(n,n,res,""); return res; } void generate(int l, int r, vector& res, string s) { if (原创 2014-09-16 19:16:29 · 838 阅读 · 0 评论 -
Reverse Nodes in k-Group[leetcode]递归和非递归的解法
题目不难,但是容易出错,需要考虑各种边界情况非递归代码如下:ListNode *reverseKGroup(ListNode *head, int k) { if (head == NULL || k <= 1) return head; ListNode * start = NULL, * end = NULL, *newHead = NULL, *p原创 2014-09-16 18:05:02 · 854 阅读 · 0 评论 -
Next Permutation[leetcode]
基本思路是从后往前找到第一个递减的数num[index],并与之前的第一个大于num[index]的数交换位置。注意交换后[index+1...n-1]仍是非递减的,所以只需要reverse一下,使其变成非递增的 void nextPermutation(vector &num) { int index = num.size() - 2; int rI原创 2014-09-16 17:07:18 · 674 阅读 · 0 评论 -
Trapping Rain Water[leetcode]
int trap(int A[], int n) { if (n == 0) return 0; int l = 0, r = n - 1; int lv = A[l], rv = A[r]; int total = A[l] + A[r], rainTotal = total; while (l != r)原创 2014-09-16 14:26:27 · 486 阅读 · 0 评论 -
Restore IP Addresses [leetcode]
这题乍一看有点像Decode Ways ,实际上是一个深搜+剪枝的题目也可以通过三个for循环寻找可行的‘.’的位置递归方法如下: vector restoreIpAddresses(string s) { vector res; restore(s, 0, 0, res, ""); return res; }原创 2014-09-26 23:09:58 · 622 阅读 · 0 评论 -
Decode Ways [leetcode] DP
题目:求将数字字符串s转换成字母有多少种可能的结果?dp(i):s[0...i-1]可能的结果数。我们设si = i-1分析s[si]:1. s[si - 1] = '1' || s[si - 1] = '2' && s[si] = '6's[0...si]可以转换成s[si]对应的字母 + s[0...si-1]对应的字母,也可能是 s[si-1..si]对应的字母 + s[0.原创 2014-09-26 22:25:13 · 1500 阅读 · 0 评论 -
Subsets II [leetcode] 从获取子集的递归和循环方法说起,解决重复子集的问题
这一题和Permutation II很像,一个是排列一个是组合。我们理清思路,从最基本的获取子集的方法开始分析:获取子集总的来说可以用循环或者递归做,同时还可以根据数字对应的binary code得到。例如s = {x,y,z}可以生成的组合有:x,y,z,xy,yz,xz,xyz,0第一种思路:1. 维护一个集合Set(i),包含s[0...i]可生成的所有组合s原创 2014-09-26 21:39:04 · 1241 阅读 · 0 评论 -
Permutations II [leetcode]
防止产生重复的排列字串,即在排列时不会重复的交换第一种思路是在递归函数中用set检查当前交换的数字是否已经换过,如{0,1,0,2}中start=0时:交换num[0]和num[0],并产生start=1的子排列交换num[0]和num[1],并产生start=1的子排列交换num[0]和num[2],并产生start=1的子排列(产生重复)交换num[0]和n原创 2014-09-16 00:06:10 · 1230 阅读 · 0 评论 -
Implement strStr() [leetcode] BM算法和KMP算法
我的实现中,BM算法在OJ中比KMP稍慢原创 2014-09-15 22:32:24 · 905 阅读 · 0 评论 -
Interleaving String [leetcode] DP
dp[k1][k2]:s1[0...k1-1]和s2[0...k2-1]能否合成s3[0...k1 + k2 - 1] bool isInterleave(string s1, string s2, string s3) { if (s3.size() != s1.size() + s2.size()) return false; vector> dp(s原创 2014-09-27 22:39:30 · 912 阅读 · 1 评论 -
Largest Rectangle in Histogram [leetcode]
O(n)的方法,基于zhan转载 2014-09-27 10:36:15 · 482 阅读 · 0 评论 -
Recover Binary Search Tree [leetcode]
本题是在中序遍历的基础上,找不合规范(不是递增)的树节点对,然后交换首先看两种序列:1. 1 3 2 4=>应该交换3和22. 4 3 2 1=>应交换4和1这两种序列对应了不符合条件的BST的中序遍历序列的所有可能性---两个节点中序遍历相邻/不相邻如果我们用一个数组swap保存所有中序遍历不递增的结果,那么这个数组只可能是2或者4的大小而我们交换数组中节点对内容,只需交原创 2014-09-27 13:28:40 · 768 阅读 · 0 评论 -
Candy [leetcode] O(n)时间复杂度,O(1)空间复杂度的方法
对于ratings[i+1],和ratings[i]的关系有以下几种:1. 相等。相等时ratings[i+1]对应的糖果数为12.ratings[i + 1] > ratings[i]。在这种情况下,要寻找以ratings[i]开始的递增序列。3.ratings[i + 1] 对于任意一个递增序列 [2 3 4 5 6] 对应的糖果数为 [1 2 3 4 X]对于任意一个递减原创 2014-10-02 10:57:18 · 976 阅读 · 0 评论 -
Max Points on a Line [leetcode]
思路:对每个点,记录原创 2014-10-01 22:06:21 · 474 阅读 · 0 评论 -
Palindrome Partitioning I,II[leetcode] DFS以及DP的解法
第一种方法是DFS,将所有可能的前缀找到,递归调用partition(剩余字符串)复杂度为O(2^n)代码如下: vector> partition(string s) { vector> res; vector patition; if (s.size() == 0) return res; partition(s原创 2014-10-09 23:01:38 · 1148 阅读 · 0 评论 -
Binary Tree Maximum Path Sum [leetcode] dp
a(i):以节点i作为终点的单边最大路径和b(i):以节点i作为终点的双边边最大路径和a(i) = max{ i->val,i->val + max{a(i->left), a(i->right) }};b(i) = max{ i->val, i->val + max{a(i->left), a(i->right) } ,i->val + a(i->left) + a(原创 2014-10-09 18:52:44 · 793 阅读 · 0 评论 -
Best Time to Buy and Sell Stock I,II,III [leetcode]
Best Time to Buy and Sell Stock I只能作一次操作时:维护preMin记录之前出现的最小值代码如下: int maxProfit(vector &prices) { if (prices.size() == 0) return 0; int profit = 0; int preMin = prices原创 2014-10-09 21:09:04 · 814 阅读 · 0 评论 -
Word Ladder II [leetcode]
本题有几个注意点:1. 回溯找路径时,根据路径的最大长度控制回溯深度2. BFS时,在找到end单词后,给当前层做标记find=true,遍历完当前层后结束。不需要遍历下一层了。3. 可以将字典中的单词删除,替代visited的set,这样优化以后时间从1700ms+降到800ms+代码如下:class Solution {public: vector> findLa原创 2014-10-09 17:45:54 · 904 阅读 · 0 评论 -
Clone Graph [leetcode] dfs和bfs
变量unordered_map cloneMap;DFS: UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) { if (node == NULL) return NULL; if (cloneMap.find(node) != cloneMap.end())原创 2014-10-08 21:35:51 · 717 阅读 · 0 评论 -
Gas Station [leetcode] 的两种解法
由于gas总量大于cost总量时,一定可以绕所有城市一圈。第一种解法:假设一开始有足够的油,从位置i出发,到位置k时剩余的油量为L(i,k)。对任意的k,L(i,k)根据i的不同,只相差常数。我们只需要找到最小的L(0, k)对应的k,k+1为所求。代码如下: int canCompleteCircuit(vector &gas, vector &cost) {原创 2014-10-08 23:21:15 · 923 阅读 · 0 评论 -
Maximal Rectangle [leetcode] 的三种思路
第一种方法是利用DP,时间复杂度是 O(m * m * n)dp(i,j):矩阵中同一行以(i,j)结尾的全部为1的最长子串长度代码如下: int maximalRectangle(vector > &matrix) { int m = matrix.size(); if (m == 0) return 0; int n = mat原创 2014-09-29 10:59:04 · 1359 阅读 · 1 评论 -
Reorder List [leetcode] 的两种思路
第一种思路是用一个vector存所有的Node*之后再用两个指针将链表拼接出来 void reorderList(ListNode *head) { vector content; ListNode * cur = head; while (cur) { content.push_back(cur原创 2014-10-07 23:43:59 · 1179 阅读 · 0 评论 -
LRU Cache [leetcode]
使用双向链表+map,实现O(1)时间内的get和set需要注意的是:1. set时更新tailsize为0时更新头部size为capacity时删除头部并且更新头部2. get时更新节点到tail的位置,同时如果是节点是头部的话要更新头部附上代码:class LRUCache{ struct Node{ int key; int原创 2014-10-07 22:50:13 · 803 阅读 · 0 评论 -
Submission Details [leetcode] 算法的改进
最先看到这一题,直觉的解法就是len从1到s1.size()-1,递归调用比较s1和s2长度为len的子串是否相等,以及剩余部分是否相等。将s1分成s1[len + rest],分别比较s2[len + rest]和s2[rest + len]代码如下: bool isScramble(string s1, string s2) { return find(s1, s2);原创 2014-09-28 12:12:44 · 1057 阅读 · 0 评论 -
Letter Combinations of a Phone Number [leetcode]谈谈循环解法的两种思路
本系列博文中有很多两种思路的,其实是因为第一遍刷题的时候有一个想法,第二遍刷题的时候已经忘掉之前的思路了,又有新的想法了。同时大部分代码我也同时PO到leetcode的对应题目的问答中去了,所以如果你也查看问题讨论的话会发现有和我一模一样的代码,其实就是我PO的:)书接正文,基于循环的两种思路如下:第一种思路比如“234”这个字符串,我可以先将0...1的所有排列找到-->原创 2014-09-16 11:29:35 · 1688 阅读 · 0 评论 -
Convert Sorted List to Binary Search Tree [leetcode] O(n)的算法
主要的思想类似中序遍历,先构建左子树,再构建当前节点,并构建右子树TreeNode *sortedListToBST(ListNode *head) { int count = 0; ListNode * cur = head; while (cur) { count++; cu原创 2014-09-26 10:58:10 · 801 阅读 · 0 评论 -
Combination Sum II [leetcode] 不用set的解法
vector > combinationSum2(vector &num, int target) { vector> res; sort(num.begin(), num.end()); vector cur; find(num, target, num.size() - 1, res, cur); return r原创 2014-09-16 16:13:45 · 467 阅读 · 0 评论 -
Longest Substring Without Repeating Characters[leetcode]
记录最大的起始位置+hashint lengthOfLongestSubstring(string s) { map charMap; int curLen, maxLen = 0,lastIndex = -1; for (int i = 0; i < s.size(); i++) { if (charMa原创 2014-09-14 11:57:42 · 995 阅读 · 0 评论 -
Two Sum[leetcode]
好久没有写博客了,最近在重新刷leetcode,发现以前自己写的相当不错的代码,再加上自己最新的一些思考,故po出来这一题解题思路有两个,一个是排序+O(n)搜索另外一个是HashHash方法的代码如下 vector twoSum(vector &numbers, int target) { map num_index; vector res;原创 2014-09-14 11:47:56 · 898 阅读 · 0 评论 -
Longest Valid Parentheses[leetcode] 的三种解法
这一题有三种解法第一种jie原创 2014-09-14 21:13:33 · 784 阅读 · 0 评论 -
Add Two Numbers[leetcode]递归版本和循环版本
我写了两个版本供参考:递归版本ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { return addTwoNumbers(l1, l2, 0); } ListNode *addTwoNumbers(ListNode *l1, ListNode *l2, int carry) {原创 2014-09-14 12:01:06 · 1289 阅读 · 0 评论 -
Median of Two Sorted Arrays[leetcode] O(nlogn)的两种思路
设两个数组分别为A和B,size为as和bs。原问题可以转化为两个排序数组求第k大的问题。还是两种思路:比较A[as/2]和B[bs/2],每次抛弃A或者B的一半代码如下:double findMedianSortedArrays(int A[], int m, int B[], int n) { int total = m + n; if (tot原创 2014-09-14 11:54:52 · 910 阅读 · 0 评论 -
Sort Colors [leetcode] 扫描数组一遍,O(1)空间复杂度的解法
扫描数组两遍的方法是:第一遍计算有每个颜色有多少个,第二遍再将所有颜色赋回数组扫描数组一遍的方法:nextPos数组中记录三种颜色的下一个位置考虑A={0,2,1,1,0}时我们应该如何更新nextPos初始:nextPos = {0,0,0}第一个颜色是0,所以nextPos[0] = 1。A={0...} 但是由于1和2必须在0的后面,所以nextPos[1], nex原创 2014-09-22 14:45:27 · 856 阅读 · 0 评论 -
Minimum Window Substring [leetcode]
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN原创 2014-09-22 14:05:19 · 481 阅读 · 0 评论 -
Word Search [leetcode]
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically原创 2014-09-22 14:04:18 · 390 阅读 · 0 评论 -
Permutation Sequence [leetcode]
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""3原创 2014-09-21 20:27:08 · 440 阅读 · 0 评论
分享