剑指offer系列--1
来源:互联网 发布:科比两次得分王数据 编辑:程序博客网 时间:2024/06/07 06:08
剑指offer系列–1
1.复杂链表的复制:每个节点含一个数据项、一个指向下一节点的指针、一个随机指向的指针;
struct RandomListNode { int label; struct RandomListNode *next, *random; RandomListNode(int x) : label(x), next(NULL), random(NULL) { }};RandomListNode* Clone(RandomListNode* pHead){ if(NULL == pHead) return NULL; //Copy: A->A'->B->B'->C->C'->NULL RandomListNode *curr = pHead, *node; while(NULL != curr) { node = new RandomListNode(curr->label); node->next = curr->next; curr->next = node; curr = node->next; } //Random curr = pHead; while(NULL != curr) { node = curr->next; if(NULL != curr->random) node->random = curr->random->next; curr = node->next; } RandomListNode *ret = pHead->next; //Split curr = pHead; while(NULL != curr->next) { node = curr->next; curr->next = node->next; curr = node; } return ret;}
2.二叉搜索树转有序双向链表:递归
struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { }};TreeNode* Convert(TreeNode* pRootOfTree){ if(NULL == pRootOfTree) return NULL; TreeNode *ret = NULL, *temp = NULL; //has left_child if(NULL != pRootOfTree->left) { ret = temp = Convert(pRootOfTree->left); while(NULL != temp->right) temp = temp->right; temp->right = pRootOfTree; pRootOfTree->left = temp; } //don't have left_child else { ret = pRootOfTree; } //has right_child if(NULL != pRootOfTree->right) { temp = Convert(pRootOfTree->right); temp->left = pRootOfTree; pRootOfTree->right = temp; } return ret;}
3.连续子序列的最大和
int FindGreatestSumOfSubArray(vector<int> array) { if(array.empty()) return 0; int sum = array[0], tempsum = array[0]; for(int i = 1; i < array.size(); i++) { tempsum = (tempsum < 0) ? array[i] : tempsum + array[i]; sum = (tempsum > sum) ? tempsum : sum; } return sum;}
0 0
- 《剑指offer》系列---1
- 剑指offer系列--1
- 《剑指offer》系列---2
- 剑指offer系列
- 算法系列-----剑指offer
- Coding_Algorithms(剑指Offer系列)
- 剑指offer系列-替换空格
- 剑指offer系列文章汇总
- 剑指offer系列----替换空格
- 剑指offer系列源码-二进制中1的个数
- 剑指offer系列源码-1+2+3+...+n
- 剑指Offer系列---(1)将字符串转换成整数
- 剑指Offer系列---(13)二进制中1的个数
- 剑指offer系列之十:二进制中1的个数
- 【剑指offer系列】 二进制中1的个数___10
- 剑指offer系列之1:二维数组中的查找
- 剑指offer系列之9:二进制中1的个数
- 剑指offer系列-T10二进制中1的个数
- Simple_window.h的使用
- 三层架构
- Windows核心编程 第八章 用户方式中线程的同步(下)
- Marklogic学习 —— 自由发问式通用索引
- SSAS 定义和部署多维数据集
- 剑指offer系列--1
- sort() 排序不影响原数组,data.concat([])
- LRU缓存
- 第一篇 设计模式之简单工厂模式
- 打开excel文件
- 手把手教你做音乐播放器(八)桌面小工具(上)
- TOMCAT JVM调优
- POJ 3661 跑步+dp
- [leetcode] 2. Add Two Numbers