Interview+Question+Answers.docx

上传人:b****6 文档编号:7767380 上传时间:2023-01-26 格式:DOCX 页数:36 大小:37.98KB
下载 相关 举报
Interview+Question+Answers.docx_第1页
第1页 / 共36页
Interview+Question+Answers.docx_第2页
第2页 / 共36页
Interview+Question+Answers.docx_第3页
第3页 / 共36页
Interview+Question+Answers.docx_第4页
第4页 / 共36页
Interview+Question+Answers.docx_第5页
第5页 / 共36页
点击查看更多>>
下载资源
资源描述

Interview+Question+Answers.docx

《Interview+Question+Answers.docx》由会员分享,可在线阅读,更多相关《Interview+Question+Answers.docx(36页珍藏版)》请在冰豆网上搜索。

Interview+Question+Answers.docx

Interview+Question+Answers

ReverseaSinglyLinkedListwithoutusingRecursion.

///

///Revertasinglelinkedlist

///

///asinglelinkedlist

///asinglelinkedlist

publicstaticSingleLinkedListNodeReverse(SingleLinkedListNoderoot)

{

if(root==null)

thrownewArgumentNullException("root");

SingleLinkedListNodeiter=root,next=root.Next;

SingleLinkedListNodenewRoot=root;

root.Next=null;

while(next!

=null)

{

newRoot=next.Next;

next.Next=iter;

iter=next;

next=newRoot;

}

returniter;

}

ReverseaSinglyLinkedListwithusingRecursion.

编程找出N以下的所有素数

voidfindPrimes(bignumtopCandidate)

{

bignumcandidate=2;

while(candidate<=topCandidate)

{

bignumtrialDivisor=2;

intprime=1;

while(trialDivisor*trialDivisor<=candidate)

{

if(candidate%trialDivisor==0)

{

prime=0;

break;

}

trialDivisor++;

}

if(prime)printPrime(candidate);

candidate++;

}

}

ReverseeverywordinaString(abcdefbecomescbafed).

1.privatestaticstringReverseStringByWord(stringinput)

2.{

3.if(string.IsNullOrEmpty(input))

4.thrownewArgumentNullException("input");

5.

6.char[]sb=newchar[input.Length];

7.intlastIndex=input.Length;

8.while((lastIndex>0)&&char.IsWhiteSpace(input[lastIndex-1]))lastIndex--;

9.

10.intappendIndex=0;

11.for(inti=lastIndex-1;i>=0;i--)

12.{

13.charc=input[i];

14.//if(char.IsWhiteSpace(input[i]))

15.if(c==''||c=='\t'||c=='\n'||c=='\r')

16.{

17.for(intj=i+1;j

18.sb[appendIndex++]=input[j];

19.

20.sb[appendIndex++]=input[i];

21.lastIndex=i;

22.}

23.}

24.

25.for(intj=0;j

26.sb[appendIndex++]=input[j];

27.

28.returnnewstring(sb);

29.}

Writeafunctionthattakesinastringparameterandcheckstoseewhetherornotitisaninteger,andifitisthenreturntheintegervalue.

1.publicstaticintParse(stringargs)

2.{

3.//Author:

YiminShi.2008-05-30

4.//这个函数也有一个BUG,比如在处理字符串"-5147483648"时候,就不会throwOverflowException.

5.if(string.IsNullOrEmpty(args))

6.thrownewArgumentNullException("args");

7.

8.intiter=0;

9.boolbNegative=false;

10.intresult=0;

11.//Skipwhites

12.while(iter

13.

14.if(args[iter]=='-')

15.{

16.bNegative=true;

17.iter++;

18.}

19.elseif(args[iter]=='+')

20.iter++;

21.

22.for(;iter

23.{

24.if(args[iter]>='0'&&args[iter]<='9')

25.{

26.intcur=args[iter]-'0';

32.

33.checked(result*=10);

34.

35.result=bNegative?

result-cur

36.:

result+cur;

42.}

43.else

44.break;

45.}

46.

47.//Skipwhites

48.while(iter

49.if(iter!

=args.Length)

50.thrownewArgumentException();

51.

52.returnresult;

53.}

Implementstrstr()(orsomeotherstringlibraryfunction).

char*__cdeclstrstr(

constchar*str1,

constchar*str2

{

char*cp=(char*)str1;

char*s1,*s2;

if(!

*str2)

return((char*)str1);

while(*cp)

{

s1=cp;

s2=(char*)str2;

while(*s1&&*s2&&!

(*s1-*s2))

s1++,s2++;

if(!

*s2)

return(cp);

cp++;

}

return(NULL);

}

Implementanalgorithmtoprintoutallfilesbelowagivenrootnode.

publicabstractclassAbstractGraph:

AbstractContainer,Graph

{

protectedintnumberOfVertices;

protectedintnumberOfEdges;

protectedVertex[]vertex;

publicvoidBreadthFirstTraversal(

Visitorvisitor,intstart)

{

bool[]enqueued=newbool[numberOfVertices];

for(intv=0;v

enqueued[v]=false;

Queuequeue=newQueueAsLinkedList();

queue.Enqueue(vertex[start]);

enqueued[start]=true;

while(!

queue.IsEmpty&&!

visitor.IsDone)

{

Vertexv=(Vertex)queue.Dequeue();

visitor.Visit(v);

foreach(Vertextoinv.Successors)

{

if(!

enqueued[to.Number])

{

queue.Enqueue(to);

enqueued[to.Number]=true;

}

}

}

}

}

现有一单链表,输出从尾端开始的第5个数值,并写出相应的测试用例

Giventwolinkedlistswhichhavedatainascendingorder,howwouldyoumergethem

///

///Mergetwosortedlinkedlistinsortedorder

///

///thefirstsortedlinkedlist

///thesecondsortedlinkedlist

///

///Theresultsortedlinkedlist

///

///Fore.g.

///SingleLinkedListNode.PrintLinkedList(SingleLinkedListNode.OrderedMerge(SingleLinkedListNode.InitializeTestList(newint[]{1,2,4,5,6,7,8,9}),SingleLinkedListNode.InitializeTestList(newint[]{3,6,8,9,10})));

///-returns-

///1->2->3->4->5->6->6->7->8->8->9->9->10->

///

///SingleLinkedListNode.PrintLinkedList(SingleLinkedListNode.OrderedMerge(SingleLinkedListNode.InitializeTestList(newint[]{1,2,4,5,7}),SingleLinkedListNode.InitializeTestList(newint[]{3,6,8,9,10})));

///-returns-

///1->2->3->4->5->6->7->8->9->10->

///

publicstaticSingleLinkedListNodeOrderedMerge(SingleLinkedListNodefirst,SingleLinkedListNodesecond)

{

if(first==null)

thrownewArgumentNullException("first");

if(second==null)

thrownewArgumentNullException("second");

SingleLinkedListNodeless=first.Value

first:

second;

SingleLinkedListNodelarger=first.Value>second.Value?

first:

second;

SingleLinkedListNodenewRoot=less;

SingleLinkedListNodeiter=newRoot;

less=less.Next;

while(less!

=null&&larger!

=null)

{

if(less.Value

{

iter.Next=less;

less=less.Next;

}

else

{

iter.Next=larger;

larger=larger.Next;

}

iter=iter.Next;

}

while(less!

=null)

{

iter=iter.Next=less;

less=less.Next;

}

while(larger!

=null)

{

iter=iter.Next=larger;

larger=larger.Next;

}

returnnewRoot;

}

Implementanalgorithmtodowildcardstringmatching.

1.privatestaticboolWildCardMatch(stringpattern,stringinput)

2.{

3.if(string.IsNullOrEmpty(pattern))

4.thrownewArgumentNullException("pattern");

5.if(string.IsNullOrEmpty(input))

6.thrownewArgumentNullException("input");

7.

8.intinputIter=0;

9.intpatternIter=0;

10.for(;patternIter

11.{

12.if(pattern[patternIter]=='*')

13.{

14.while((patternIter

15.if(patternIter==pattern.Length-1)

16.returntrue;

17.

18.while((patternIter

19.(inputIter

20.(pattern[patternIter+1]!

=input[inputIter]))

21.{

22.inputIter++;

23.}

24.}

25.else

26.{

27.if(inputIter>=input.Length)break;

28.

29.if(pattern[patternIter]!

=input[inputIter++])

30.returnfalse;

31.}

32.}

33.

34.returnpatternIter==pattern.Length&&inputIter==input.Length;

35.}

Writearoutinethatprintsouta2-Darrayinspiralorder!

    intleft=0,top=0,right=3,bottom=3,CurrentRow=0,CurrentCulom=0,step;

           int[,]Num=newint[,]{{1,2,3,4},{ 5,6,7,8},{9,10,11,12},{13,14,15,16}};

           //ruledefine:

{X,Y},ifX=0,traversalcolum,ifX=1,traversalrow;ifY=1,increasebydegrees,ifY=-1,decreasebydegrees

           //int[,]rule=newint[,]{{1,1},{0,1},{1,-1},{0,-1}};//clockwise

           int[,]rule=newint[,]{{0,1},{1,1},{0,-1},{1,-1}}; //counter-clockwise

           inta=Num.Rank;

           intb=Num.Length;

           intprocess=0;

           while(true)

           {

               switch(rule[process,0])

               {

                   case1:

//row

                       step=rule[process,1];

                       for(;CurrentCulom>=left&&CurrentCulom<=right;CurrentCulom+=step)

                       {

                           Console.WriteLine(Num[CurrentRow,CurrentCulom]);

                       }

                       CurrentCulom-=step;

                       if(CurrentRow==top)

                       {

                           top++;

                           CurrentRow++;

                       }

                       else

                       {

                           bottom--;

                           CurrentRow--;

                       }

                           break;

                   case0:

//colum

                           step=rule[process,1];

                           for(;CurrentRow>=top&&CurrentRow<=bottom;CurrentRow+=step)

                           {

                               Console.WriteLine(Num[CurrentRow,CurrentCulom]);

                           }

                           CurrentRow-=step;

                           if(CurrentCulom==left)

                           {

                               CurrentCulom++;

                               left++;

                           }

                           else

                           {

                               CurrentCulom--;

                               right--;

                           }

                       break;

               }

               if(top>bottom||left>right)

                   break;

               process=(process+1)%4;

           }

       }

Implementastring.Replace.

1.publicstaticstringReplace(stringstrSrc,stringoldStr,stringnewStr)

2.{//Implementastring.Replace.

3.

4.if(string.IsNullOrEmpty(strSrc)||string.IsNullOrEmpty(oldStr)||newStr==null)

5.throw

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1