11 2014 档案

摘要:给出一个已排序的数组,将其转化为二叉查找树(BST)。思路:取数组中间元素为根结点的value,则数组左侧、右侧分别为BST的左子树、右子树。递归可求解。代码如下: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * ... 阅读全文
posted @ 2014-11-26 16:14 bournet 阅读(141) 评论(0) 推荐(0) 编辑
摘要:根据二叉树的前序遍历和中序遍历构造二叉树。思路:前序遍历的第一个节点就是根节点,扫描中序遍历找出根结点,根结点的左边、右边分别为左子树、右子树中序遍历。再计算左子数长度leftLength,前序遍历根结点后的leftLength长度为左子树的前序遍历,剩下的为右子树的前序遍历,代码如下: 1 /**... 阅读全文
posted @ 2014-11-26 10:48 bournet 阅读(125) 评论(0) 推荐(0) 编辑
摘要:问题描述:n个台阶,每次只能跨1个或者2个,求有多少种方法。ways[i]:i个台阶的方法。可能是由i-1个台阶跨1个或者由i-2个台阶,转化为斐波那契数列,即1 int climbStairs(int n)2 {3 if (n == 1)4 return 1;5 e... 阅读全文
posted @ 2014-11-20 09:57 bournet 阅读(130) 评论(0) 推荐(0) 编辑
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,... 阅读全文
posted @ 2014-11-19 16:43 bournet 阅读(104) 评论(0) 推荐(0) 编辑
摘要:题目https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/对每个节点递归。 1 /** 2 * Definition for binary tree with next pointer. 3 * ... 阅读全文
posted @ 2014-11-19 11:07 bournet 阅读(125) 评论(0) 推荐(0) 编辑
摘要:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1... 阅读全文
posted @ 2014-11-18 15:50 bournet 阅读(123) 评论(0) 推荐(0) 编辑
摘要:描述有一个长为n的数列a0,a1,…,an-1。求出这个序列中最长的上升子序列长度。上升子序列指的是对于任意的i < j,都满足ai < aj的子序列。输入n = 5a = {4,2,3,1,5}输出3(a1,a2,a4构成的子序列2,3,5最长)这个问题是被称作最长上升子序列的著名问题。这一问题通... 阅读全文
posted @ 2014-11-15 20:36 bournet 阅读(239) 评论(0) 推荐(0) 编辑
摘要:有n种重量和价值分别为wi,vi的物品。从这些物品中挑选总重量不超过W的物品,求出挑选物品价值总和的最大值。每种物品可以挑选任意多件。令dp[i+1][j]:=从前i种物品中挑选总重量不超过j时总价值的最大值。那么递推关系为:根据递推关系编写代码: 1 void solve() 2 { 3 ... 阅读全文
posted @ 2014-11-15 17:23 bournet 阅读(256) 评论(0) 推荐(0) 编辑