资源描述
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<!-- saved from url=(0062)>
<HTML xmlns=""><HEAD><TITLE>微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META content=面试Interview,微软的22道数据结构算法面试题,含答案 name=keywords><LINK
href="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/common.css"
type=text/css rel=stylesheet><LINK id=MainCss
href="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/style.css" type=text/css
rel=stylesheet><LINK
href="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/common2.css"
type=text/css rel=stylesheet><LINK
href="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/shCore.css"
type=text/css rel=stylesheet><LINK
href="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/shThemeDefault.css"
type=text/css rel=stylesheet><LINK title=RSS
href="" type=application/rss+xml
rel=alternate><LINK title=RSD href=""
type=application/rsd+xml rel=EditURI>
<SCRIPT src="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/jquery.js"
type=text/javascript></SCRIPT>
<SCRIPT src="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/common.js"
type=text/javascript></SCRIPT>
<SCRIPT
src="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/jquery.json-2.2.min.js"
type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2900.5897" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 action=1393081.html method=post>
<DIV><INPUT id=" __VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV><!--done-->
<DIV id=home>
<DIV id=header>
<DIV id=blogTitle><!--done-->
<DIV class=title><A class=headermaintitle id=Header1_HeaderTitle
href="">Alex iPhone 之旅</A></DIV>
<DIV class=subtitle>动手,分享,交流</DIV></DIV><!--end: blogTitle 博客的标题和副标题 -->
<DIV id=navigator><!--done-->
<UL id=navList>
<LI><A class=menu id=MyLinks1_HomeLink href="">博客园</A>
</LI>
<LI><A class=menu id=MyLinks1_MyHomeLink
href="">首页</A> </LI>
<LI><A href="">新闻</A> </LI>
<LI><A class=menu id=MyLinks1_NewPostLink
href="">新随笔</A> </LI>
<LI><A class=menu id=MyLinks1_ContactLink accessKey=9
href="">联系</A> </LI>
<LI><A class=menu id=MyLinks1_Admin
href="">管理</A> </LI>
<LI><A class=menu id=MyLinks1_Syndication
href="">订阅</A> <A class=aHeaderXML
id=MyLinks1_XMLLink href=""><IMG
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
alt=订阅 src="微软的22道数据结构算法面试题(含答案) - Alex iPhone 之旅 - 博客园.files/rss.gif"></A>
</LI></UL>
<DIV class=blogStats><!--done-->随笔-156 文章-1 评论-358 </DIV><!--end: blogStats --></DIV><!--end: navigator 博客导航栏 --></DIV><!--end: header 头部 -->
<DIV id=main>
<DIV id=mainContent>
<DIV class=forFlow><!--done-->
<DIV id=topics>
<DIV class=post>
<H1 class=postTitle><A class=postTitle2 id=ctl04_TitleUrl
href="">微软的22道数据结构算法面试题(含答案)</A>
</H1>
<DIV class=clear></DIV>
<DIV class=postBody>
<P><BR><STRONG> 1、反转一个链表。循环算法。</STRONG> <BR>
<BR> <BR> 1 List reverse(List
l) { <BR> 2 if(!l) return
l; <BR> 3 list cur
= l.next; <BR> 4 list pre
= l; <BR> 5 list tmp;
<BR> 6 pre.next = null;
<BR> 7 while ( cur ) {
<BR> 8 tmp = cur;
<BR> 9 cur =
cur.next; <BR> 10 tmp.next =
pre <BR> 11 pre =
tmp; <BR> 12 } <BR> 13
return tmp; <BR> 14 } <BR>
<BR><STRONG> 2、反转一个链表。递归算法。</STRONG> <BR> <BR> 1
List resverse(list l) { <BR> 2
if(!l || !l.next) return l;
<BR> 3 <BR> 4
List n = reverse(l.next); <BR> 5
l.next.next = l; <BR> 6
l.next=null; <BR> 7 }
<BR> 8 return n; <BR> 9 }
<BR> <BR> <BR><STRONG>
3、广度优先遍历二叉树。</STRONG> <BR> 1 void
BST(Tree t) { <BR> 2 Queue
q = new Queue(); <BR> 3
q.enque(t); <BR> 4 Tree t
= q.deque(); <BR> 5
while(t) { <BR> 6
System.out.println(t.value); <BR> 7
q.enque(t.left); <BR> 8
q.enque(t.right); <BR> 9 t
= q.deque(); <BR> 10 }
<BR> 11 } <BR> ----------------------
<BR> 1class Node { <BR> 2
Tree t; <BR> 3 Node next;
<BR> 4 } <BR> 5class Queue
{ <BR> 6 Node head;
<BR> 7 Node tail; <BR> 8
public void enque(Tree t){ <BR>
9 Node n = new
Node(); <BR> 10 n.t = t;
<BR> 11 if(!tail){ <BR> 12
tail = head =
n; <BR> 13 } else
{ <BR> 14 tail.next = n;
<BR> 15 tail = n;
<BR> 16 } <BR> 17 }
<BR> 18 public Tree deque() {
<BR> 19 if (!head) {
<BR> 20
return null; <BR> 21 }
else { <BR> 22 Node n
= head; <BR> 23 head
= head.next; <BR> 24 return
n.t; <BR> 25 } <BR>
26} </P>
<P><STRONG>4、输出一个字符串所有排列。注意有重复字符。</STRONG> <BR> <BR>
1char[] p; <BR> 2void perm(char
s[], int i, int n){ <BR> 3
int j; <BR> 4 char temp;
<BR> 5 for(j=0;j<n;++j){ <BR> 6
if(j!=0 && s[j]==s[j-1]); <BR>
7 elseif(s[j]!='@'){ <BR> 8
p[i]=s[j]; <BR> 9
s[j]='@'; <BR> 10 if(i==n-1){
<BR> 11 p[n]='\0'; <BR> 12
printf("%s", p); <BR> 13
}else{ <BR> 14 perm(s,i+1,n);
<BR> 15 } <BR> 16
s[j]=p[i]; <BR> 17 } <BR> 18
} <BR> 19} <BR> --------------------------
<BR> 1void main() { <BR> 2
char s[N]; <BR> 3 sort(s); <BR> 4
perm(s,0,strlen(s)); <BR> 5} <BR>
<BR> <BR><STRONG> 5、输入一个字符串,输出长型整数。</STRONG>
<BR> <BR> 1 long atol(char *str){
<BR> 2 char *p =
str; <BR> 3 long
l=1;m=0; <BR> 4 if
(*p=='-') { <BR> 5
l=-1; <BR> 6
++p; <BR> 7
} <BR> 8
while(isDigit(*p)){ <BR> 9
m = m*10 + p;
<BR> 10 ++p;
<BR> 11 } <BR> 12
if(!p) return m*l; <BR> 13
else return error; <BR>
14} <BR> <BR> <BR><STRONG>
6、判断一个链表是否有循环。</STRONG> <BR> <BR> 1
int isLoop(List l) { <BR>
2 if ( ! l)
return - 1 ; <BR>
3 List s =
l.next; <BR> 4
while (s && s != l)
{ <BR> 5
s = s.next;
<BR> 6 } <BR> 7
if ( ! s)
return - 1 ; <BR>
8 else reutrn
1 ; <BR> 9 } <BR>
----------- <BR> 1int isLoop(List l){
<BR> 2 if(!l) return 0;
<BR> 3 p=l.next;
<BR> 4 wihle(p!=l&&p!=null)
{ <BR> 5
l.next=l; <BR> 6
l=p;p=p.next; <BR> 7
} <BR> 8
if(p=l) return 1; <BR> 9
return 0; <BR> 10} <BR>
实际上,在我的面试过程中,还问到了不破坏结构的其他算法。 <BR>
我的答案是从链表头开始遍历,假如节点next指针指向自身,则循环存在;否则将next指针指向自身,遍历下一个节点。直至next指针为空,此时链表无循环。
<BR> <BR> <BR><STRONG>
7、反转一个字符串。</STRONG> <BR> <BR> 1
void reverse( char *
str) { <BR> 2
char tmp; <BR> 3
int len; <BR> 4
len =
strlen(str); <BR> 5
for ( int i = 0
;i < len / 2 ; ++ i)
{ <BR> 6
tmp = char [i]; <BR>
7 str[i]
= str[len - i - 1
]; <BR> 8
str[len - i - 1 ] =
tmp; <BR> 9 }
<BR> 10 } <BR>
<BR><STRONG> 8、实现strstr函数。</STRONG> <BR> <BR>
1int strstr(char[] str, char[] par){
<BR> 2 int i=0;
<BR> 3 int j=0;
<BR> 4 while(str[i] &&
str[j]){ <BR> 5
if(str[i]==par[j]){ <BR> 6
++i; <BR> 7
++j; <BR> 8
}else{ <BR> 9
i=i-j+1; <BR> 10
j=0; <BR> 11
} <BR> 12
} <BR> 13
if(!str[j]) return i-strlen(par); <BR> 14
else return -1; <BR> 15} </P>
<P><STRONG>9、实现strcmp函数。</S
展开阅读全文