9.1Youaregiventwosortedarrays,AandB,andAhasalargeenoughbufferattheendtoholdB.WriteamethodtomergeBintoAinsortedorder.Option1.CreateanextrabigarrayC.iteratebothAandB.returnC.O(m+n)Option2.AssumeAisbigenough.IterateAandBstartingfromend.PutmaxvaluetotheendofA.i..
分类:
其他好文 时间:
2014-12-04 06:30:19
阅读次数:
118
8.6Implementthe“paintfill”functionthatonemightseeonmanyp_w_picpatheditingprograms.Thatis,givenascreen(representedbya2dimensionalarrayofColors),apoint,andanewcolor,fillinthesurroundingareauntilyouhitaborderofthatcolor.interfacePanter
{
}
classPos
{
intx,
..
分类:
其他好文 时间:
2014-12-02 12:00:20
阅读次数:
116
8.4Writeamethodtocomputeallpermutationsofastring.ThisisaverysimilarquestiontoCC8.3
staticCollection<String>permutations(Strings)
{
if(s==null||s.isEmpty())
returnCollections.emptyList();
if(s.length()==1)
returnCollections.singletonList..
分类:
其他好文 时间:
2014-12-01 16:14:11
阅读次数:
165
8.3Writeamethodthatreturnsallsubsetsofaset.powerSet(i)=
[powerSet(i-1)]*ITEMi+//Addnewitemintoeachexistingset
[pwerSet(i-1)]+//Existingset
ITEMi//singlenewitem.
powerSet(1)=ITEM1.
<T>Set<Set<T>>powerSet(Set<T>set)
{
if(set==null||se..
分类:
其他好文 时间:
2014-12-01 16:14:01
阅读次数:
147
8.3Writeamethodthatreturnsallsubsetsofaset.powerSet(i)=
[powerSet(i-1)]*ITEMi+//Addnewitemintoeachexistingset
[pwerSet(i-1)]+//Existingset
ITEMi//singlenewitem.
powerSet(1)=ITEM1.
<T>Set<Set<T>>powerSet(Set<T>set)
{
if(set==null||se..
分类:
其他好文 时间:
2014-12-01 10:16:15
阅读次数:
146
8.2ImaginearobotsittingontheupperlefthandcornerofanNxNgrid.Therobotcanonlymoveintwodirections:rightanddown.Howmanypossiblepathsaretherefortherobot?FOLLOWUPImaginecertainsquaresare“offlimits”,suchthattherobotcannotsteponthem.Designanalgorithmtogetallpossib..
分类:
其他好文 时间:
2014-12-01 10:16:04
阅读次数:
118
8.1WriteamethodtogeneratethenthFibonaccinumber.classFibonacci()
{
voidinit()
{
a=0;
b=1;
}
intnext()
{
inttoReturn=a+b;
a=b;
b=toReturn;
returntoReturn;
}
}
{
Fibonaccifibo=init();
for(n)
{
toReturn=fibo.next();
}
returntoReturn;
}
fibonacci(intn)
{
if(..
分类:
其他好文 时间:
2014-11-28 10:30:25
阅读次数:
149
4.7Youhavetwoverylargebinarytrees:T1,withmillionsofnodes,andT2,withhundredsofnodes.CreateanalgorithmtodecideifT2isasubtreeofT1.//Itwilliteratemaintree(millions)nodes.
//Toobad.
booleanisSubTree(Nodemain,Nodesub)
{
if(main==null)
returnnull;
if(main==sub)
r..
分类:
其他好文 时间:
2014-11-28 10:30:24
阅读次数:
157
3.5ImplementaMyQueueclasswhichimplementsaqueueusingtwostacks.interfaceQueue<T>
{
enqueue(Tt);
Tdequeue();
}
classMyQueue<T>implementsQueue<T>
{
MyQueue()
{
//Initstackss1ands2;
...
}
//O(n)
enqueue(Tt)
{
while(!s2.isEmpty())
{
s1.push(s2..
分类:
其他好文 时间:
2014-11-27 08:05:14
阅读次数:
140
3.6Writeaprogramtosortastackinascendingorder.Youshouldnotmakeanyassumptionsabouthowthestackisimplemented.Thefollowingaretheonlyfunctionsthatshouldbeusedtowritethisprogram:push|pop|peek|isEmpty.interfaceStack<T>
{
push(Tt);
Tpop();
Tpeek();
booleanisEm..
分类:
其他好文 时间:
2014-11-27 08:02:46
阅读次数:
156