日复一日

厚积薄发|跳跃的人生

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

2006年6月17日

摘要: 不重复地输出升序数组中的元素。1 void outputUnique( char []str, int n) {2 if (n <= 0 ) return ;3 elseif(n == 1 )putchar(str[ 0 ]);4 else {5 int i = 0 ,j = 1 ;6 putchar(str[ 0 ]);7 while (j < n) {8 if (str[j... 阅读全文
posted @ 2006-06-17 08:54 GwQ 阅读(1143) 评论(0) 推荐(1)

摘要: 面试过程中我还遇到了下面几题:1、如何删除链表的倒数第m的元素?我的方法是先用pre指针从链表头开始步进m,新建pst节点next指针指向头节点,cur指针指向头节点,然后pre,cur,post三个指针一起步进,当pre指向链表结尾的时候cur指向倒数第m个元素,最后利用pst指针删除cur指向元素。2、如何判断一个字符串是对称的?如a,aa,aba。设置头尾指针同时向中间比较靠齐直至相遇。3、... 阅读全文
posted @ 2006-06-17 08:54 GwQ 阅读(1592) 评论(1) 推荐(0)

摘要: 用一个数据结构实现 1if (x == 0) y = a;2else y = b; 1j[] = {a,b};2y=j[x]; 阅读全文
posted @ 2006-06-17 08:53 GwQ 阅读(760) 评论(0) 推荐(0)

摘要: 在双向链表中删除指定元素。 1void del(List head, List node){ 2 List pre=new List(); 3 pre.next = head; 4 List cur = head; 5 while(cur && cur!=node){ 6 cur=cur.next; 7 pre=pre.next; 8 }... 阅读全文
posted @ 2006-06-17 08:53 GwQ 阅读(795) 评论(0) 推荐(0)

摘要: 两个链表,一升一降。合并为一个升序链表。 1 List merge(List a, List d) { 2 List a1 = reverse(d); 3 List p = q = new List(); 4 while ( a && a1 ) { 5 if (a.value < a1.value) { 6 ... 阅读全文
posted @ 2006-06-17 08:52 GwQ 阅读(758) 评论(4) 推荐(0)

摘要: 将长型转换为字符串。 1char* ltoa(long l){ 2 char[N] str; 3 int i=1,n=1; 4 while(!(l/i<10)){i*=10;++n} 5 char* str=(char*)malloc(n*sizeof(char)); 6 int j=0; 7 while(l){ 8 str[j++]=l/i;... 阅读全文
posted @ 2006-06-17 08:52 GwQ 阅读(751) 评论(2) 推荐(0)

摘要: 判断一颗二叉树是否平衡。 1int isB(Tree t){2 if(!t) return 0;3 int left=isB(t.left);4 int right=isB(t.right);5 if( left >=0 && right >=0 && left - right =-1)6 return (left<right)? (right +1) : (... 阅读全文
posted @ 2006-06-17 08:51 GwQ 阅读(657) 评论(0) 推荐(0)

摘要: 返回一颗二叉树的深度。 1int depth(Tree t){2 if(!t) return 0;3 else {4 int a=depth(t.right);5 int b=depth(t.left);6 return (a>b)?(a+1):(b+1);7 }8} 阅读全文
posted @ 2006-06-17 08:51 GwQ 阅读(692) 评论(0) 推荐(0)

摘要: 在一个链表中删除另一个链表中的元素。 1void delete(List m, List n) { 2 if(!m || !n) return; 3 List pre = new List(); 4 pre.next=m; 5 List a=m, b=n,head=pre; 6 while(a && b){ 7 if(a.value b.value)... 阅读全文
posted @ 2006-06-17 08:50 GwQ 阅读(691) 评论(0) 推荐(0)

摘要: 一个数组,下标从0到n,元素为从0到n的整数。判断其中是否有重复元素。 1int hasDuplicate(int[] a, int n){ 2 for(int i=0;i<n;++i){ 3 while(a[i]!=i && a[i]!=-1){ 4 if(a[a[i]]==-1) return 1; 5 a[i]=a[a[i]]... 阅读全文
posted @ 2006-06-17 08:50 GwQ 阅读(743) 评论(1) 推荐(0)

摘要: 汉诺塔问题。 1void tower(n,x,y,z){2 if(n==1) move(x,z);3 else {4 tower(n-1, x,z,y);5 move(x,z);6 tower(n-1, y,x,z);7 }8} 阅读全文
posted @ 2006-06-17 08:49 GwQ 阅读(801) 评论(0) 推荐(0)

摘要: 三柱汉诺塔最小步数。 1 int f3(n) { 2 if (f3[n]) return f3[n]; 3 else { 4 if (n == 1 ) { 5 f3[n] = 1 ; 6 return 1 ; 7 } 8 f3[n] = 2 * f... 阅读全文
posted @ 2006-06-17 08:49 GwQ 阅读(736) 评论(0) 推荐(0)

摘要: 求一个整形中1的位数。 1 int f( int x) {2 int n = 0 ;3 while (x) {4 ++ n;5 x &= x - 1 ;6 } 7 return n;8 } 阅读全文
posted @ 2006-06-17 08:34 GwQ 阅读(930) 评论(2) 推荐(0)

摘要: 实现strcmp函数。 1int strcmp(char* str1, char* str2){2 while(*str1 && *str2 && *str1==*str2){3 ++str1;4 ++str2;5 }6 return *str1-*str2;7} 阅读全文
posted @ 2006-06-17 08:33 GwQ 阅读(763) 评论(0) 推荐(0)

摘要: 反转一个字符串。 1 void reverse( char * str) { 2 char tmp; 3 int len; 4 len = strlen(str); 5 for ( int i = 0 ;i < len / 2 ; ++ i) { 6 tmp = char [i]; 7 str[i] ... 阅读全文
posted @ 2006-06-17 08:32 GwQ 阅读(918) 评论(4) 推荐(0)

摘要: 实现strstr函数。 1int strstr(char[] str, char[] par){ 2 int i=0; 3 int j=0; 4 while(str[i] && str[j]){ 5 if(str[i]==par[j]){ 6 ++i; 7 ++j; 8 }else{ 9 ... 阅读全文
posted @ 2006-06-17 08:32 GwQ 阅读(777) 评论(0) 推荐(0)

摘要: 判断一个链表是否有循环。 1 int isLoop(List l) {2 if ( ! l) return - 1 ;3 List s = l.next;4 while (s && s != l) {5 s = s.next;6 } 7 if ( ! s) return - 1 ;8 e... 阅读全文
posted @ 2006-06-17 08:31 GwQ 阅读(984) 评论(6) 推荐(0)

摘要: 广度优先遍历二叉树。 1void BST(Tree t) { 2 Queue q = new Queue(); 3 q.enque(t); 4 Tree t = q.deque(); 5 while(t) { 6 System.out.println(t.value); 7 q.enque(t.left); 8 q.enque(t.right); 9 t = ... 阅读全文
posted @ 2006-06-17 08:28 GwQ 阅读(2187) 评论(2) 推荐(0)

摘要: 反转一个链表。递归算法。 1List resverse(list l) {2 if(!l || !l.next) return l;3 4 List n = reverse(l.next);5 l.next.next = l;6 l.next=null;7 }8 return n;9} 阅读全文
posted @ 2006-06-17 08:28 GwQ 阅读(2406) 评论(0) 推荐(0)

摘要: 前段时间找工作,针对网上能搜集到的微软试题进行了准备。现分享如下,如有谬误,还希望希望朋友能多多指出。朋友们如果有其他未收录的题目,也可以在这里分享。1、反转一个链表。循环算法。 1List reverse(List l) { 2 if(!l) return l; 3 list cur = l.next; 4 list pre = l; 5 list tmp; 6 pre.next... 阅读全文
posted @ 2006-06-17 08:26 GwQ 阅读(5163) 评论(18) 推荐(0)