剑指offer 习题讲解与复习

在升序数组中查找。

bool Find(int** matrix,int row,int col,int value)

{
bool found = false;
bool jump = false;
int needle = 0;
for(int i = col-1;i>=0&&jump;i--)
{
needle++;
for(int j = 0+needle;j {
if(value = matrix[j][i])
{
jump = true;
found = true;
break;
}
else if(value>matrix[j][i])
continue;
else
break;
}
}
return found;

}

链表的一系列操作

struct Node
{
int value;
Node* next;
};
void AddNodeToTail(Node** pHhead,int value)
{
Node* tempHead = *pHhead;
tempHead->value = value;
tempHead->next = NULL;
Node* node = new Node();
if(NULL==tempHead)//空链表
{
*pHhead = tempHead;
return;
}
else
{
while(NULL!=tempHead->next)
{
tempHead = tempHead->next;
}
tempHead->next = node;
}
}
void RemoveNode(Node** pHead,int value)
{
if(NULL == pHead || NULL == *pHead)
return;
Node* beToDelete = NULL;
Node* tempHead = *pHead;
if(value == tempHead->value)
{
*pHead = (*pHead)->next;
delete tempHead;
tempHead = NULL;
return;
}
Node* prev = NULL;
while(tempHead->next!=NULL&&tempHead->next->value != value)
{
tempHead = tempHead->next;
}
if(tempHead->next!=NULL&&tempHead->next->value == value)
{
beToDelete = tempHead ->next;
tempHead->next = beToDelete->next;
delete beToDelete;
}
}
void PrintListFromTail(Node* pHead)
{
if(NULL == pHead)
return;
std::stack stackInt;
Node* tempHead = pHead;
while(NULL!=tempHead)
{
stackInt.push(tempHead->value);
tempHead = tempHead->next;
}
while(stackInt.size()>0)
{
printf("%d ",stackInt.top());
stackInt.pop();
}
}
void PrintListRecursive(Node* pHead)
{
Node* tempHead = pHead;
if(NULL!=tempHead)
{
if(NULL!= tempHead->next)
PrintListRecursive(tempHead->next);
printf("%d\t",tempHead->value);
}
}

树的操作

树的遍历都是以根节点为参考点的

float 精确到小数点7位,但是只负责到6位,第7位并不保证

打印从1到N的所有数字的组合

bool IsOverFlow(char* toBePrint)
{
bool result = false;
if(NULL!=toBePrint)
{
if('1'==toBePrint[0])
result = true;
}
return result;
}
void Increase(char* beToPrint,int length)
{
if(length<=0||NULL==beToPrint)
throw std::exception("invalid param");
if(NULL!=beToPrint)
{
int needle = length-2;
while('9'==beToPrint[needle])
{
beToPrint[needle--] = '0';
}
beToPrint[needle]++;
}
}


void PrintBigData(int n)
{
char* toBePrint = new char[n+2];
memset(toBePrint,'0',n+2);
toBePrint[n+1] = 0;
while(!IsOverFlow(toBePrint)&&n>0)
{
Increase(toBePrint,n+2);
if('1'!=toBePrint[0])
{
bool beginPrint = false;
for(int i = 1;i{
if('0'!=toBePrint[i])
beginPrint  = true;
if(beginPrint)
printf("%c",toBePrint[i]);
}
printf("\n");
}
}
delete []toBePrint;
toBePrint = NULL;
}

你可能感兴趣的:(剑指OFFER)