05 2014 档案
摘要:原题地址:https://oj.leetcode.com/problems/gas-station/题意:There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You ha...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/candy/题意:There areNchildren standing in a line. Each child is assigned a rating value.You are giving candies to ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/word-break-ii/题意:Given a stringsand a dictionary of wordsdict, add spaces insto construct a sentence where each ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/word-break/题意:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/max-points-on-a-line/题意:Givennpoints on a 2D plane, find the maximum number of points that lie on the same strai...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/题意:Evaluate the value of an arithmetic expression inReverse Polish Notation.Val...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/reverse-words-in-a-string/题意:Given an input string, reverse the string word by word.For example,Given s = "the s...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/median-of-two-sorted-arrays/题意:There are two sorted arrays A and B of size m and n respectively. Find the median...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/permutations-ii/题意:Given a collection of numbers that might contain duplicates, return all possible unique permu...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/permutations/题意:Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the foll...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/palindrome-partitioning-ii/题意:Given a strings, partitionssuch that every substring of the partition is a palindr...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/题意:Given a strings, partitionssuch that every substring of the partition is a palindrome...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/subsets-ii/题意:Given a collection of integers that might contain duplicates,S, return all possible subsets.Note:E...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/subsets/题意:枚举所有子集。解题思路:碰到这种问题,一律dfs。代码:class Solution: # @param S, a list of integer # @return a list of l...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/combinations/题意:组合求解问题。解题思路:这种求组合的问题,需要使用dfs来解决。代码:class Solution: # @return a list of lists of integers d...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/minimum-path-sum/题意:Given amxngrid filled with non-negative numbers, find a path from top left to bottom right w...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/climbing-stairs/题意:You are climbing a stair case. It takesnsteps to reach to the top.Each time you can either cl...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/clone-graph/题意:实现对一个图的深拷贝。解题思路:由于遍历一个图有两种方式:bfs和dfs。所以深拷贝一个图也可以采用这两种方法。不管使用dfs还是bfs都需要一个哈希表map来存储原图中的节点和新图中的节点的一...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/path-sum-ii/题意:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/path-sum/题意:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees-ii/题意:接上一题,这题要求返回的是所有符合条件的二叉查找树,而上一题要求的是符合条件的二叉查找树的棵数,我们上一题提过,求个数一般思路是动态规划,而枚举的话,我们就考...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/unique-binary-search-trees/题意:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/n-queens-ii/题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题。解题思路:上道题使用了递归回溯的解法,这道题我们可以使用非递归回溯来解决,因为如果使用递归回溯来解决,那么代码...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/n-queens/题意:经典的N皇后问题。解题思路:这类型问题统称为递归回溯问题,也可以叫做对决策树的深度优先搜索(dfs)。N皇后问题有个技巧的关键在于棋盘的表示方法,这里使用一个数组就可以表达了。比如board=[1, ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/symmetric-tree/题意:判断二叉树是否为对称的。Given a binary tree, check whether it is a mirror of itself (ie, symmetric around ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/same-tree/题意:判断两棵树是否是同一棵树。解题思路:这题比较简单。用递归来做。首先判断两个根节点的值是否相同,如果相同,递归判断根的左右子树。代码:# Definition for a binary tree n...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/validate-binary-search-tree/题意:检测一颗二叉树是否是二叉查找树。解题思路:看到二叉树我们首先想到需要进行递归来解决问题。这道题递归的比较巧妙。让我们来看下面一棵树: ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/recover-binary-search-tree/题意:Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/题意:Given a binary tree, find the maximum path sum.The path may start and end at any...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/题意:Follow up for problem "Populating Next Right Pointers in Each ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/题意: 1 / \ 2 3 / \ / \ 4 5 6 7变为: ...
阅读全文
摘要:原题地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/题意:A linked list is given such that each node contains an additional random pointe...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡。解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的。解题思路:由于要求二叉查找树是平衡的。所以我们可以选在数组的中间那...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/题意:Given a binary tree, return thebottom-up level ordertraversal of its node...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/minimum-depth-of-binary-tree/题意:Given a binary tree, find its minimum depth.The minimum depth is the number of no...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/题意:Given a binary tree, return thezigzag level ordertraversal of its nod...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/题意:二叉树的层序遍历的实现。解题思路:二叉树的层序遍历可以用bfs或者dfs来实现。这里使用的dfs实现,代码比较简洁。实际上,二叉树的先序遍历就是dfs实...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/sum-root-to-leaf-numbers/题意:Given a binary tree containing digits from0-9only, each root-to-leaf path could repre...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/题意:Given a binary tree, flatten it to a linked list in-place.For example,Given...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/题意:实现后序遍历。递归实现比较简单,非递归实现。解题思路:这道题的迭代求解比先序遍历和后序遍历要麻烦一些。假设一棵树是这样的: ...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/题意:这题用递归比较简单。应该考察的是使用非递归实现二叉树的先序遍历。先序遍历的遍历顺序是:根,左子树,右子树。解题思路:如果树为下图: ...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/题意:二叉树的中序遍历。这道题用递归比较简单,考察的是非递归实现二叉树中序遍历。中序遍历顺序为:左子树,根,右子树。如此递归下去。解题思路:假设树为: ...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/balanced-binary-tree/题意:判断一颗二叉树是否是平衡二叉树。解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于1。这实际上是AVL树的定义。首先要写一个计算二叉树高度的函...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/题意:根据二叉树的先序遍历和中序遍历恢复二叉树。解题思路:可以参照http://www.cnblogs.com...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/题意:根据二叉树的中序遍历和后序遍历恢复二叉树。解题思路:看到树首先想到要用递归来解题。以这道题为例:如果一...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/single-number-ii/题意:Given an array of integers, every element appearsthreetimes except for one. Find that single ...
阅读全文
摘要:原题地址:http://www.cnblogs.com/x1957/p/3373994.html题意:Given an array of integers, every element appearstwiceexcept for one. Find that single one.要求:线性时间复...
阅读全文
摘要:原题地址:http://oj.leetcode.com/problems/valid-number/题意:判断输入的字符串是否是合法的数。解题思路:这题只能用确定有穷状态自动机(DFA)来写会比较优雅。本文参考了http://blog.csdn.net/kenden23/article/detail...
阅读全文