版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
PAGE微軟的22道數(shù)據(jù)結(jié)構(gòu)算法面試題(含答案)
1、反轉(zhuǎn)一個(gè)鏈表。循環(huán)算法。
1
List
reverse(List
l)
{
2
if(!l)
return
l;
3
list
cur
=
l.next;
4
list
pre
=
l;
5
list
tmp;
6
pre.next
=
null;
7
while
(
cur
)
{
8
tmp
=
cur;
9
cur
=
cur.next;
10
tmp.next
=
pre;
11
pre
=
tmp;
12
}
13
return
tmp;
14
}
2、反轉(zhuǎn)一個(gè)鏈表。遞歸算法。
1
List
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
}
3、廣度優(yōu)先遍歷二叉樹(shù)。
1
void
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
=
q.deque();
10
}
11
}
1class
Node
{
2
Tree
t;
3
Node
next;
4
}
5class
Queue
{
6
Node
head;
7
Node
tail;
8
public
void
enque(Tree
t){
9
Node
n
=
new
Node();
10
n.t
=
t;
11
if(!tail){
12
tail
=
head
=
n;
13
}
else
{
14
tail.next
=
n;
15
tail
=
n;
16
}
17
}
18
public
Tree
deque()
{
19
if
(!head)
{
20
return
null;
21
}
else
{
22
Node
n
=
head;
23
head
=
head.next;
24
return
n.t;
25
}
26}
4、輸出一個(gè)字符串所有排列。注意有重復(fù)字符。
1char[]
p;
2void
perm(char
s[],
int
i,
int
n){
3
int
j;
4
char
temp;
5
for(j=0;j<n;++j){
6
if(j!=0
&&
s[j]==s[j-1]);
7
elseif(s[j]!='@'){
8
p[i]=s[j];
9
s[j]='@';
10
if(i==n-1){
11
p[n]='\0';
12
printf("%s",
p);
13
}else{
14
perm(s,i+1,n);
15
}
16
s[j]=p[i];
17
}
18
}
19}
1void
main()
{
2
char
s[N];
3
sort(s);
4
perm(s,0,strlen(s));
5}
5、輸入一個(gè)字符串,輸出長(zhǎng)型整數(shù)。
1
long
atol(char
*str){
2
char
*p
=
str;
3
long
l=1;m=0;
4
if
(*p=='-')
{
5
l=-1;
6
++p;
7
}
8
while(isDigit(*p)){
9
m
=
m*10
+
p;
10
++p;
11
}
12
if(!p)
return
m*l;
13
else
return
error;
14}
6、判斷一個(gè)鏈表是否有循環(huán)。
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
else
reutrn
1
;
9
}
1int
isLoop(List
l){
2
if(!l)
return
0;
3
p=l.next;
4
wihle(p!=l&&p!=null)
{
5
l.next=l;
6
l=p;p=p.next;
7
}
8
if(p=l)
return
1;
9
return
0;
10}
實(shí)際上,在我的面試過(guò)程中,還問(wèn)到了不破壞結(jié)構(gòu)的其他算法。
我的答案是從鏈表頭開(kāi)始遍歷,如果節(jié)點(diǎn)next指針指向自身,則循環(huán)存在;否則將next指針指向自身,遍歷下一個(gè)節(jié)點(diǎn)。直至next指針為空,此時(shí)鏈表無(wú)循環(huán)。
7、反轉(zhuǎn)一個(gè)字符串。
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]
=
str[len
-
i
-
1
];
8
str[len
-
i
-
1
]
=
tmp;
9
}
10
}
8、實(shí)現(xiàn)strstr函數(shù)。
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
i=i-j+1;
10
j=0;
11
}
12
}
13
if(!str[j])
return
i-strlen(par);
14
else
return
-1;
15}9、實(shí)現(xiàn)strcmp函數(shù)。
1int
strcmp(char*
str1,
char*
str2){
2
while(*str1
&&
*str2
&&
*str1==*str2){
3
++str1;
4
++str2;
5
}
6
return
*str1-*str2;
7}
10、求一個(gè)整形中1的位數(shù)。
1
int
f(
int
x)
{
2
int
n
=
0
;
3
while
(x)
{
4
++
n;
5
x
&=
x
-
1
;
6
}
7
return
n;
8
}
11、漢諾塔問(wèn)題。
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}
12、三柱漢諾塔最小步數(shù)。
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
*
f3(n
-
1
)
+
1
;
9
return
f3[n];
10
}
11
}
四柱漢諾塔最小步數(shù)。
1int
f4(n){
2
if(f4[n]==0){
3
if(n==1)
{
4
f4[1]==1;
5
return
1;
6
}
7
min=2*f4(1)+f3(n-1);
8
for(int
i=2;i<n;++i){
9
u=2*f4(i)+f3(n-i);
10
if(u<min)
min=u;
11
}
12
f4[n]=min;
13
return
min;
14
}
else
return
f4[n];
15}
13、在一個(gè)鏈表中刪除另一個(gè)鏈表中的元素。
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)
{
8
a=a.next;
9
pre=pre.next;
10
}else
if(a.value
>
b.value){
11
b=b.next;
12
}else{
13
a=a.next;
14
pre.next=a;
15
}
16
}
17
m=head.next;
18}
14、一個(gè)數(shù)組,下標(biāo)從0到n,元素為從0到n的整數(shù)。判斷其中是否有重復(fù)元素。
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]];
6
a[a[i]]=-1;
7
}
8
if(a[i]==i)
{a[i]=-1;}
9
}
10
return
0;
11}
15、判斷一顆二叉樹(shù)是否平衡。
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
||
left
-right
>=-1)
6
return
(left<right)?
(right
+1)
:
(left
+
1);
7
else
return
-1;
8}
9
16、返回一顆二叉樹(shù)的深度。
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}
17、兩個(gè)鏈表,一升一降。合并為一個(gè)升序鏈表。
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
p.next
=
a;
7
a
=
a.next;
8
}
else
{
9
p.next
=
a1;
10
a1
=
a1.next;
11
}
12
p
=
p.next;
13
}
14
if
(a)
p.next
=
a;
15
elseif(a1)
p.next
=
a1;
16
return
q.next;
17
}
18、將長(zhǎng)型轉(zhuǎn)換為字符串。
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;
9
l=l%i;
10
i/=10;
11
}
12
return
str;
13}
19、用一個(gè)數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)
1
if
(x
==
0)
y
=
a;
2
else
y
=
b;
1
j[]
=
{a,b};
2
y=j[x];
20、在雙向鏈表中刪除指定元素。
1void
del(List
head,
List
node){
2
List
pre=new
List();
3
pre.next
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年度個(gè)人與公司租賃合同稅費(fèi)承擔(dān)協(xié)議4篇
- 二零二五版金融服務(wù)保密協(xié)議范本修訂6篇
- 2025年保定怎么考貨運(yùn)從業(yè)資格證
- 二零二五年城投小貸與農(nóng)業(yè)產(chǎn)業(yè)合作框架協(xié)議4篇
- 2025年度農(nóng)村土地流轉(zhuǎn)經(jīng)營(yíng)權(quán)抵押貸款合同示范文本4篇
- 二零二五年度充電樁安裝工程知識(shí)產(chǎn)權(quán)保護(hù)合同4篇
- 二零二五年度出境領(lǐng)隊(duì)旅游目的地考察合同4篇
- 二零二五年度城市綜合體建設(shè)項(xiàng)目承包商安全作業(yè)管理協(xié)議4篇
- 2025年度葡萄采摘季節(jié)臨時(shí)工采購(gòu)合同范本3篇
- 二零二五年度企業(yè)知識(shí)產(chǎn)權(quán)運(yùn)營(yíng)管理合同-@-2
- 垃圾處理廠(chǎng)工程施工組織設(shè)計(jì)
- 天皰瘡患者護(hù)理
- 2025年蛇年新年金蛇賀歲金蛇狂舞春添彩玉樹(shù)臨風(fēng)福滿(mǎn)門(mén)模板
- 《建筑制圖及陰影透視(第2版)》課件 4-直線(xiàn)的投影
- 新生物醫(yī)藥產(chǎn)業(yè)中的人工智能藥物設(shè)計(jì)研究與應(yīng)用
- 防打架毆斗安全教育課件
- 損失補(bǔ)償申請(qǐng)書(shū)范文
- 壓力與浮力的原理解析
- 鐵路損傷圖譜PDF
- 裝修家庭風(fēng)水學(xué)入門(mén)基礎(chǔ)
- 移動(dòng)商務(wù)內(nèi)容運(yùn)營(yíng)(吳洪貴)任務(wù)二 社群的種類(lèi)與維護(hù)
評(píng)論
0/150
提交評(píng)論