- 博客(16)
- 资源 (12)
- 收藏
- 关注
原创 判断单链表是否存在环有问题
bool hasCycle(ListNode *head) { if (!head) { return false; } ListNode *p = head->next; while (true) { if (!p) {
2014-07-19 16:42:59 491
原创 bst的serialize和unserialize
void serialize(node *root, ofstream &file) { if (!root) return; file dat << " "; serialize(root->l, file); serialize(root->r, file); } node * unserialize(ifstream &file) { node * r
2014-07-16 15:35:27 561
原创 单链表反序递归版本和迭代版本
/* 将单链表反序 */ node *reverse_iterative(node *L) { if (!L) return NULL; node *head, *tail, *p; head = tail = L; p = L->next; head->next = NULL; while (p) { head = p; p = p->next;
2014-07-16 15:30:55 489
原创 判定二叉树是否是BST 递归版本
/* 判定二叉树是否是BST * BST * 注意体会第二参数的意义:前一个被访问节点的值,需要使用引用 * * 时间复杂度O(n),需要逐个遍历树中的元素。 * 思路: bst的中序序列是严格递增的 * */ bool is_bst_by_in(node *root, int& preval) { if (!root) return true; /
2014-07-16 10:10:20 637
原创 计算二叉树的高度中序迭代版本,有错误待定位
/** * 计算二叉树的高度 * 根节点的高度为1,空节点的高度0 * 采用中序迭代的方式进行求解 * * 树的高度就是运行时栈的最大深度 * 该想法经过验证有问题,对于没有左 * 树的情况不对。 * **/ int height_in_iterative(node *root) { if (!root) return 0; int h = 0; int
2014-07-16 09:51:11 469
原创 二叉树中序遍历的迭代版本
/*** 二叉树遍历的中序的迭代版本 * 需要stack 结构, cur变量的帮助 * 每个节点都作为左子树被压入栈,作为根被访问, * 再转入右子树做重复的处理 * ***/ void in_visit_iterative(node *root) { if (!root) return; node *cur = root; sta
2014-07-16 09:49:37 2548
原创 计算二叉树的高度递归版本
/** * 计算二叉树的高度 * 根节点的高度为1,空节点的高度0 * 采用递归的方式进行求解 * **/ int height(node *root) { int h = 0; if (!root) return 0; int h1 = height(root->l); int h2 = height(root->r); if (h1
2014-07-16 09:47:39 1714
原创 判断一个整数是不是2的整数幂
/* 判断一个整数是不是2的整数幂 */ bool is_power_of_two(int x) { return !x & (x - 1); /* bug here, can you fix it?? */ }
2014-07-16 09:00:12 593
原创 从文件读取数据构建单链表,返回链表头指针
/* * 从文件读取数据构建单链表,返回链表头指针。 * 以正序方式构建链表需要两个变量 head, tail * * 有没有更加简洁的方式?? */ node * list(ifstream &file) { int n; node *head = NULL, *tail = NULL; int v; file >> n; file >> v;
2014-07-15 16:24:58 2448
原创 判断单链表L1 L2是否相交
/* * 判断单链表L1 L2是否相交 * 1. 将L1链接成环。 * 2. 如果相交,从L2出发一定能够访问到L1. * * 还有一种很直观的算法,借助额外的存储空间。 */ bool is_intersection(node *L1, node *L2) { if (!L1 || !L2) return true; node * h = L1; /* 将L1
2014-07-15 16:24:00 463
原创 计算整数x的二进制表示中1的个数
int count_one_bruteforce(int x) { int n = sizeof(int) * 8; int c = 0; for (int i = 0; i < n; i++) { if ((x >> i ) & 1) { c++; } } return c; }
2014-07-14 21:48:23 1153
原创 单链表的分隔
void partition_bruteforce(node *head, node * &front, node * &back) { if (!head) { front = NULL; back = NULL; return; } int n = 0; node *p = NULL; for (p = head; p; p = p->next) /
2014-07-14 20:20:40 462
原创 通过二叉树的前序序列和后续序列构建二叉树,有问题带定位
node* build_tree_by_pre_in(int *pre, int *in, int n) { if (0 == n) return NULL; int v = pre[0]; int i = map[v]; node *root = new node(v); root->l = build_tree_by_pre_in(pre + 1, in, i);
2014-07-13 21:49:21 340
原创 单向循环链表的有序插入 对单向循环链表head插入元素 链表保持有序
#ifndef CYCLICLIST_H_#define CYCLICLIST_H_#include using namespace std;class CyclicList{public: /* public标示外部代码可见 */ struct node { int dat; struct node* next; }; /* * 向循环单链表中插入新元素
2014-07-01 21:32:37 1588
原创 反序一个无符号整数x 不占用额外的空间 采用异或操作
#ifndef BITSREVERSE_H_#define BITSREVERSE_H_#includeusing namespace std;class Bit{public: typedef unsigned int uint; /* * 交换无符号数字x的i,j位 * 曾出现两处书写错误 1. 进行异或交换时候,构造bit时候>. 2.循环交换时候n/2误写
2014-07-01 21:29:15 560
原创 内存泄露错误
/* * 释放单向循环链表中的全部元素,并将链表头指针设置为空。 * 使用局部变量cur,nxt。 * 下面的程序中存在内存泄露 想以一种简洁的方式进行更正,但是目前还 * 没有想到。 */ void free(struct node* &head) { if (NULL == head) return; struct node *cur, *nxt;
2014-07-01 11:41:07 506
grub启动内核和initrd.img
2012-03-20
linux loop device的用途
2012-03-19
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人