摘要: improt os #os模块提供操作文件盒目录的函数os.getcwd() #获取脚本运行的目录os.listdir(os.getcwd()) #取得目录中的内容['DLLs', 'Doc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'mymodule', 'mytest.pyc', 'NEWS.txt', 'python.exe', 'pythonw.exe', 'R 阅读全文
posted @ 2013-03-28 20:30 iyjhabc 阅读(243) 评论(0) 推荐(0)
摘要: 第一题:把二分查找树转换为升序排序的双向链表。不能新建节点,只能改变树的指针。二分查找树:左小于父,右大于父。中序遍历就是升序遍历。中序遍历递归法:void showMidTree(BSTreeNode *pRoot){ if(pRoot!=NULL) { showMidTree(pRoot->m_pLeft); cout<<pRoot->m_nValue<<endl; showMidTree(pRoot->m_pRight); }}思路一:当我们到达某一结点准备调整以该结点为根结点的子树时,先调整其左子树将左 子树转换成一个排好序的... 阅读全文
posted @ 2013-03-28 09:40 iyjhabc 阅读(473) 评论(0) 推荐(0)
摘要: 第十一题:求二元树中两个节点最远的距离方法:使用递归函数求左右子树的深度。左子树深度+右子树深度=最大距离。int BinaryTreeNode::getDeep(int floor){//(也是形参传递参数,返回值传递结果) int leftDeep=this->m_pLeft!=NULL ? this->m_pLeft->getDeep(floor+1) : floor; int rightDeep=this->m_pRight!=NULL ? this->m_pRight->getDeep(floor+1) : floor; return leftDe 阅读全文
posted @ 2013-03-28 09:36 iyjhabc 阅读(284) 评论(0) 推荐(0)
摘要: 第二十一题:输入m、n,打印出1~n中所有和等于m的组合(01背包问题)递归法:void printSolution(int *flag){ for(int i=1;i<100;++i) if(flag[i]!=0)cout<<i<<" "; cout<<endl;}void bao(int m,int n,int *flag){ if(m<1 || n<1)return; if(n>m)n=m; if(n==m){//end , print it flag[n]=1; printSolution(flag); . 阅读全文
posted @ 2013-03-28 09:34 iyjhabc 阅读(212) 评论(0) 推荐(0)
摘要: 第三十六题:比赛排名void match(int w[][8],int *order,int *result,int n){ memcpy(result,order,n*sizeof(int)); int round=n; while(round>0){ for(int i=0,j=0;i<round;i+=2,++j){ if(w[result[i]][result[i+1]]==result[i])swap(i,j,result); else swap(i+1,j,result); }... 阅读全文
posted @ 2013-03-28 09:32 iyjhabc 阅读(262) 评论(0) 推荐(0)
摘要: 第四十三题:二叉树的非递归前中后序遍历前序遍历:void preorderShowTreeIterative(BSTreeNode *pRoot){ stack<BSTreeNode*> buf; buf.push(pRoot); while(!buf.empty()){ BSTreeNode *p=buf.top(); buf.pop(); cout<<p->m_nValue<<endl; if(p->m_pRight!=NULL)buf.push(p->m_pRight);//right first i... 阅读全文
posted @ 2013-03-28 09:30 iyjhabc 阅读(211) 评论(0) 推荐(0)
摘要: 第五十一题:输出所有和为n的连续正数序列方法1:从1遍历到n/2,算出所有sum,如果sum刚好等于n则输出,效率极低。方法2:用一个变量b标志连续序列的开头,e表示结尾。如果序列的sum<n,则e++;如果sum>n,则b++。复杂度O(n)void constSumSequence(int n){ int b=1,e=1,sum=1,bound=n/2+1; while(e<=bound){ if(sum==n){ for(int k=b;k<=e;++k)cout<<k<<" "; cout<<endl; . 阅读全文
posted @ 2013-03-28 09:28 iyjhabc 阅读(208) 评论(0) 推荐(0)
摘要: 第六十一题:找出整型数组中两个只出现了一次的数(其余都出现了两遍)思路:凡是涉及到出现了两次,出现了一次的都用XOR。把所有的数都XOR一遍得到一个数tmp,这个数就是要得到的两个数的XOR。tmp肯定不为0,因为这要得的两个数一定不相等。则使用tmp为1的位可以把原数组分为两组。因为这两个数的这一位一定不同(1^0=1),所以这两个数一定各自落在一个组里,而出现了两次的同一个数也会在同一组里。重新对各个组XOR一编即可找到这个组中唯一出现一次的数了。整个过程遍历了两次数组,复杂度O(2n)=O(n)void twoSingleNum(const int *a,int cnt){ int... 阅读全文
posted @ 2013-03-28 09:24 iyjhabc 阅读(236) 评论(0) 推荐(0)
摘要: 第七十一题:求a的b次方思路1:循环乘积思路2:half=a^(b/2) 即a^b=half*half,复杂度从O(n)降为O(logn)PS:除以二可以用>>1代替,判断可以否整除2可以用 & 1代替,位操作提高系效率。double helper(double base,int power){ if(power==1)return base; if(power==-1)return 1/base; if(power%2==0){ double half=helper(base,power/2); return half*half; }els... 阅读全文
posted @ 2013-03-28 08:57 iyjhabc 阅读(442) 评论(0) 推荐(0)