EricYang

Tech Spot of Eric

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

2012年5月4日

摘要: 输出等差数列由小到大?如果没有符合条件的就输出“NO”。例如:输入[1,3,0,5,-1,6] ,输出[-1,1,3,5] 。要求:时间复杂度,空间复杂度尽量小解体思路:设随机数为a[],首先排序,(1)枚举两个数,作为数列中相邻的两个元素Ai,Ai+1,那么可求出等差d=Ai+1-Ai。于是Ai-1=Ai-d,Ai+2=Ai+1+d,因此可以在剩下的数寻找Ai-1,Ai+2是否存在,可以用一个set来维护查找,而数列最长可为n,查找次数最大为n,而查找复杂度为logn,总时间复杂度为O(n^3logn)。复杂度太高,不可取。(2)动态规划求解。设f[i][j]为以a[i],a[j]结尾的等差 阅读全文
posted @ 2012-05-04 21:14 Eric-Yang 阅读(910) 评论(0) 推荐(0)

摘要: 只要找到这样一个节点:已知的两个节点一个在它的左边子树,一个在它的右边子树;或者这个节点就是已知的两个节点中的一个,而另一个恰好在它的下面。TREE* CommonFather(TREE *root, TREE *A, TREE *B){ if(root == NULL) return root; if(root == A)//如果找到A,则后面的都不再找了,如果其他分支没找到B,则B必定在A下面 return A; if(root == B)//同上 return B; TREE *leftChild == NULL; ... 阅读全文
posted @ 2012-05-04 17:27 Eric-Yang 阅读(579) 评论(0) 推荐(0)

摘要: 题目:输入一个字符串,打印出该字符串中字符的所有排列。例如输入字符串abc,则输出由字符a、b、c所能排列出来的所有字符串abc、acb、bac、bca、cab和cba。#include <iostream>#include <string>#include <cstring>using namespace std;bool isVisited[3];void dfs(string str, string rlt, const int len, int cur){ if(cur==len) { cout<<rlt<<endl; ret 阅读全文
posted @ 2012-05-04 10:56 Eric-Yang 阅读(175) 评论(0) 推荐(0)

摘要: 规定只有根节点,树高为1;int treeHeight;void treeHigh(Node *p, int depth){ if(!p) { if(depth>treeHeight) { treeHeight=depth; } return; } treeHigh(p->lc,depth+1); treeHigh(p->rc,depth+1);}int treeHigh1(Node *p){ int hl,hr; if(!p) { return 0; ... 阅读全文
posted @ 2012-05-04 10:09 Eric-Yang 阅读(260) 评论(0) 推荐(0)