Recently read a book on the two-face test and then realized a bit of feeling good ideas, Daniel skipped:
1, for a two-dimensional matrix, from left to right from top to bottom are incremented, how to determine whether a value is inside the matrix? (c implementation of implementation complexity O (n))
BOOL Findintwodimensionalmatrix (int*pmatrix,int irows,int icols,int ifindval) { bool bfind=false; if (pmatrix==0| | irows<=0| | icols<=0) return bfind; int irow=0,icol=icols-1; while (irow<irows&&icol>=0) { if (pmatrix[irow*icols+icol]==ifindval) { bfind= true; break; } else if (pmatrix[irow*icols+icol]>ifindval) --icol; else ++irow; } return bfind;}
2, in a memory large enough space, a string of characters stored in the sequence, replace the space in which is%20? (c implementation, time complexity O (n))
void Replacecharinenoughmemory (char*pstr) { if (pstr==0) return; Calculates the number of spaces int nspace=0; int Nlen=strlen (PSTR); char *pbehand,*pfront; char *ptem=pstr; Calculates the number of spaces for (;;) { //0 end ' *ptem== ' if (') ' Break ; else if (*ptem==0x20) { ++nspace; } ptem++; } The rear equals ' pbehand=pstr+nlen-1+nspace*2 ' needs to be added ; pfront=pstr+nlen-1; for (;;) { if (*pfront!=0x20) { *pbehand=*pfront; } else{ *pbehand= ' 0 '; * (--pbehand) = ' 2 '; * (--pbehand) = '% '; } if (PFRONT==PSTR) break ; pbehand--; pfront--; } End * (pstr+nlen-1+nspace*2+1) = ' + ';}
Well, write it down here.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Two algorithm questions about ordered two-dimensional matrix lookup and string substitution