20.11Imagineyouhaveasquarematrix,whereeachcellisfilledwitheitherblackorwhite.Designanalgorithmtofindthemaximumsubsquaresuchthatallfourbordersarefilledwithblackpixels.//Abruteforcesolution.
//n*n
//iteratefromnto1.
//Forn,findallpossiblesubsquare
EnumColor..
分类:
其他好文 时间:
2014-12-14 18:43:36
阅读次数:
225
20.8GivenastringsandanarrayofsmallerstringsT,designamethodtosearchsforeachsmallstringinT.KMPBMFancymethod.
分类:
其他好文 时间:
2014-12-12 16:54:50
阅读次数:
123
20.9Numbersarerandomlygeneratedandpassedtoamethod.Writeaprogramtofindandmaintainthemedianvalueasnewvaluesaregenerated.classMedianNum
{
//O(n)
voidinsert(intn)
{
if(size==0)
{
head=newNode(n);
media=head;
}
elseif(n<head.num)
{
NodenewHead=newNode(n);
ne..
分类:
其他好文 时间:
2014-12-12 16:49:31
阅读次数:
151
20.3Writeamethodtorandomlygenerateasetofmintegersfromanarrayofsizen.Eachelementmusthaveequalprobabilityofbeingchosen.//Similarto20.2
//Thisassumem<=n
分类:
其他好文 时间:
2014-12-11 10:36:40
阅读次数:
132
19.11Designanalgorithmtofindallpairsofintegerswithinanarraywhichsumtoaspecifiedvalue.
//Assumeaisnotnull.
//
//aisnotsorted.
//
//Option1isusingaset.
List<Pair<Integer,Integer>>sumUpTo(int[]a,intsum)
{
//Option1
Map<Integer>set=newHashSet&..
分类:
其他好文 时间:
2014-12-11 10:36:22
阅读次数:
168
20.1Writeafunctionthataddstwonumbers.Youshouldnotuse+oranyarithmeticoperators.//SorryIdon‘tknow.Ihatequestionslikethis.
分类:
其他好文 时间:
2014-12-11 10:35:54
阅读次数:
133
20.2Writeamethodtoshuffleadeckofcards.Itmustbeaperfectshuffle-inotherwords,each52!permutationsofthedeckhastobeequallylikely.Assumethatyouaregivenarandomnumbergeneratorwhichisperfect.//Randomlygenerateaintkfrom1(inclusive)ton(inclusive).
intrandom(intn);
cl..
分类:
其他好文 时间:
2014-12-11 10:35:50
阅读次数:
165
20.4Writeamethodtocountthenumberof2sbetween0andn.//Whatthismean?
//Givenan.
//for(inti=0->n)
//{
//result+=numOf2In(i);
//}
//Thisispurelyamathproblem.
分类:
其他好文 时间:
2014-12-11 10:34:40
阅读次数:
234
20.6Describeanalgorithmtofindthelargest1millionnumbersin1billionnumbers.Assumethatthecomputermemorycanholdallonebillionnumbers.//canholdallnumbers.
//Cheating!
//Doweknowthemaxormin?
//Considerusingbitmap
//If1billionnumbersalsospreadalot.
//1.Hash-spliti..
分类:
其他好文 时间:
2014-12-11 10:32:53
阅读次数:
136
//Seehttp://www.hawstein.com/posts/19.1.html
//19.1Writeafunctiontoswapanumberinplacewithouttemporaryvariables.
classCC19_1
{
voidswap()
{
inta;
intb;
a=a+b;
b=a-b;
a=a-b;
}
//or
voidswap()
{
inta;
intb;
a=a^b;
b=a^b;
a=a^b;
}
}
分类:
其他好文 时间:
2014-12-10 14:26:30
阅读次数:
145