摘要: 题目链接:https://leetcode.com/problems/n-queens-ii/N皇后问题的位运算实现。 1 class Solution 2 { 3 public: 4 int totalNQueens(int n) 5 { 6 upperLimit ... 阅读全文
posted @ 2015-04-05 22:41 meowcherry 阅读(153) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/binary-tree-postorder-traversal/(非递归实现)二叉树的后序遍历。 1 class Solution 2 { 3 public: 4 vector postorderTraversal(Tre... 阅读全文
posted @ 2015-04-05 22:14 meowcherry 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/binary-tree-preorder-traversal/(非递归实现)二叉树的先序遍历。 1 class Solution 2 { 3 public: 4 vector preorderTraversal(TreeN... 阅读全文
posted @ 2015-04-05 22:13 meowcherry 阅读(127) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/binary-tree-inorder-traversal/(非递归实现)二叉树的中序遍历。 1 class Solution 2 { 3 public: 4 vector inorderTraversal(TreeNod... 阅读全文
posted @ 2015-04-05 22:12 meowcherry 阅读(117) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/maximum-product-subarray/较麻烦的解法:先判断数组中有没有0,有的话可以转换为求"0之前"和"0之后"两部分的较大值。遍历,记录数组中负数的个数:偶数:由于数组中都是整数,最大值即这些值的乘积。奇数:有以下... 阅读全文
posted @ 2015-04-05 22:11 meowcherry 阅读(101) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/unique-binary-search-trees/从最简单的情况开始考虑:n = 0:只有“空树”这一种情况。f[0]= 1。n = 1:只有“根节点”这一种情况。f[1]= 1。n = 2:由于“根节点”必须存在,所以只有一... 阅读全文
posted @ 2015-04-05 22:10 meowcherry 阅读(141) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/linked-list-cycle/判断一个链表中是否有环,可以设置两个指针,第一个每次前进一格,第二个每次前进两格。对于前进两格的指针,需要先判断前进一格时是否为 NULL :如果不判断是不是 NULL ,取两次 next 可能... 阅读全文
posted @ 2015-04-05 22:09 meowcherry 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/binary-tree-maximum-path-sum/题中要求 maxPathSum(TreeNode *root) 函数返回二叉树中最大的"Path Sum"。 而这个最大的"Path Sum"可以被分解为三部分之和:"左子... 阅读全文
posted @ 2015-04-05 22:08 meowcherry 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/当 k ≥ prices.size()/ 2 时:题目等价于 k 无限大的情形。当 k < prices.size()/ 2 时:用dp[m][n+1]表示在[... 阅读全文
posted @ 2015-04-05 22:07 meowcherry 阅读(126) 评论(0) 推荐(0) 编辑