Interview+Question+Answers.docx
《Interview+Question+Answers.docx》由会员分享,可在线阅读,更多相关《Interview+Question+Answers.docx(36页珍藏版)》请在冰豆网上搜索。
![Interview+Question+Answers.docx](https://file1.bdocx.com/fileroot1/2023-1/23/60a5b957-3e41-4bac-9ec2-b1e5887e2e9b/60a5b957-3e41-4bac-9ec2-b1e5887e2e9b1.gif)
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;j18.sb[appendIndex++]=input[j];
19.
20.sb[appendIndex++]=input[i];
21.lastIndex=i;
22.}
23.}
24.
25.for(intj=0;j26.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(iter13.
14.if(args[iter]=='-')
15.{
16.bNegative=true;
17.iter++;
18.}
19.elseif(args[iter]=='+')
20.iter++;
21.
22.for(;iter23.{
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(iter49.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;venqueued[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.Valuefirst:
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(;patternIter11.{
12.if(pattern[patternIter]=='*')
13.{
14.while((patternIter15.if(patternIter==pattern.Length-1)
16.returntrue;
17.
18.while((patternIter19.(inputIter20.(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