19.2Designanalgorithmtofigureoutifsomeonehaswoninagameoftic-tac-toe.classTicTacToe
{
enumTic
{
X,O
}
//GivenTicTacToemap,wheterthaswonthegame.
//Assumemapisanot-null3*3matrix,containingnonullelements.
//
//checkrow,checkcolumn,checkcorner
//Thisisabrutefor..
分类:
其他好文 时间:
2014-12-10 14:26:06
阅读次数:
187
19.4Writeamethodwhichfindsthemaximumoftwonumbers.Youshouldnotuseif-elseoranyothercomparisonoperator.intmax(inta,intb)
{
int[]temp={a,b};
//Ifa>b,(a-b)>>31willbe0...000000;
//Else,itwillbe11111111..1;
//Thus,
intk=((a-b)>>31)&1;
returntem..
分类:
其他好文 时间:
2014-12-10 14:22:58
阅读次数:
174
9.5Givenasortedarrayofstringswhichisinterspersedwithemptystrings,writeamethodtofindthelocationofagivenstring.Example:find“ball”in[“at”,“”,“”,“”,“ball”,“”,“”,“car”,“”,“”,“dad”,“”,“”]willreturn4Example:find“ballcar”in[“at”,“”..
分类:
其他好文 时间:
2014-12-05 19:50:00
阅读次数:
135
9.4Ifyouhavea2GBfilewithonestringperline,whichsortingalgorithmwouldyouusetosortthefileandwhy?Whatarethecommonsortingalgorithms?http://en.wikipedia.org/wiki/Sorting_algorithmMergesort,divideandconquerQuicksort,usingapivot.allnumbersbiggerthanpivotgoesoneside..
分类:
其他好文 时间:
2014-12-05 11:00:30
阅读次数:
164
9.2Writeamethodtosortanarrayofstringssothatalltheanagramsarenexttoeachother.Useamap,thekeyissortedstring,valueislistofanagramsusingcharsinthekey.List<String>sortByAnagrams(List<String>strings)
{
Map<String,List<String>>map;
for(Stri..
分类:
其他好文 时间:
2014-12-04 10:22:12
阅读次数:
128
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.2ImaginearobotsittingontheupperlefthandcornerofanNxNgrid.Therobotcanonlymoveintwodirections:rightanddown.Howmanypossiblepathsaretherefortherobot?FOLLOWUPImaginecertainsquaresare“offlimits”,suchthattherobotcannotsteponthem.Designanalgorithmtogetallpossib..
分类:
其他好文 时间:
2014-12-01 10:16:04
阅读次数:
118