首先,归并排序,分治,递归解决小的范围,再合并两个有序的小范围数组,便得到整个有序的数组。
这是很适合用递归来写的,至于非递归,便是从小到大,各个击破,从而使得整个数组有序。代码如下:
void merge(vector &A, int left, int mid, int right)
{
int i=left,j=mid+1;
vector tmp(right-left+1,0);...
分类:
其他好文 时间:
2014-08-10 13:08:00
阅读次数:
235
这篇文章的主题是动态规划, 主要介绍LeetCode中一维动态规划的题目, 列表如下: Climbing StairsDecode WaysUnique Binary Search TreesMaximum SubarrayBest Time to Buy and Sell Stock在介绍上述具体题目之前, 我们先说说动态规划的通常思路。 动态规划是一种算法思路(注意这里不要和递归混淆, 事实上...
分类:
其他好文 时间:
2014-08-10 13:03:50
阅读次数:
180
O(n) complexity, have a traversal for the tree. Get the information of all children, then traverse the tree again.
#include
#include
#include
#include
using namespace std;
class Node {
pub...
分类:
其他好文 时间:
2014-08-10 13:00:30
阅读次数:
238
error: 'transform' is not a member of 'std'labelReader.cpp:54:9:error:'transform' is not a member of 'std'build-binary.mk:386:recipe for target labelR...
分类:
移动开发 时间:
2014-08-10 12:35:20
阅读次数:
272
题意:给你一个数为n的区间,区间的起始价值为1,然后要进行m次操作,操作即为改变给定区间的值(范围为1-3),要你计算最终的权值
思路:就是线段树的区间跟新了
AC代码:
#include
#include
using namespace std;
struct node
{
int value;
int a,b;
}tree[300010];
void maketre...
分类:
其他好文 时间:
2014-08-10 10:27:20
阅读次数:
310
注:后序遍历是较麻烦的一个,不可大意。关键两点: 1.要走到 p->left | p->right ==0, 2.每次出栈出两个结点。
分类:
其他好文 时间:
2014-08-10 01:45:39
阅读次数:
286
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
zigzag层序遍历树
For example:
Given binary...
分类:
其他好文 时间:
2014-08-09 23:19:59
阅读次数:
363
题目:UVA - 10304Optimal Binary Search Tree(递推)
题目大意:给出一组数,e1 2 n,现在要求将这些数组成一棵二叉搜索树,并且使得sum (ei * cost(ei))最小。cost(ei)表示ei到到根节点之间有多少条边。
解题思路:首先二叉搜索树要满足左节点小于根节点,右节点大于根节点。因此对于e1 2 n这样一组数,我们只要枚举根节...
分类:
其他好文 时间:
2014-08-09 23:19:09
阅读次数:
266