# 1.最大公共子串

char* longest(const char* str1,const char* str2){
int len1 = strlen(str1);
int len2 = strlen(str2);
int max = 0.num = 0;
int start, start1, start2;
for(int i=0;i<len1;i++){
for(int j=0;j<len2;j++){
start1 = i;
start2 = j;
while((start1<len1)&&(start2<len2)&&(str1[start1++]==str2[start2++])){
num++;
}

if(num>max){
max = num;
start = i;
}
num = 0;
}
}

char* strnew = new char[max];
strncpy(strnew,str1+start,max);

return strnew;
}


# 2.判断回文数

bool palindromic(unsigned int n){
unsigned int m = 0,tep = n;
while(tep>0){
m = m*10 + tep%10;
tep/=10;
}

return (m==n);
}


# 3.不使用库函数将字符串转换成数字

int str2int(const char* str)
{
int temp = 0;
const char* ptr = str;

if ( *str == '-' || *str == '+' )
{
str++;
}

while ( *str != '\0' )
{
if ( ( *str < '0') || ( *str > '9' ) )
{
break;
}

temp = temp * 10 + (*str - '0');
str++;
}

if ( *ptr == '-' )
{
temp = -temp;
}

return temp;
}


# 4.C++的四种类型转换

• const_cast：去const属性;
• static_cast：基本类型转换;
• dynamic_cast：多态类之间的类型转换;
• reinterpret_cast：不同类型的指针类型转换;

# 5.删除两个双向链表中相同的节点

struct Node
{
int data;
Node *front, *next;
};

{
if(pHeadA == NULL || pHeadB == NULL)
return ;

std::map<int,Node *> mapNode;
Node *tmp = pHeadA;
while(NULL!=tmp)
{
mapNode[tmp->data] = tmp;
tmp = tmp->next;
}
Node *tmpb = pHeadB;
while(NULL!=tmpb)
{
if(mapNode[tmpb->data] != NULL)
{
//存在相同data，从A和B中删除
{
}
else
{
tmpb->front->next = tmpb->next;
tmpb->next->front = tmpb->front;
}
delete tmpb;

Node *tmpa = mapNode[tmpb->data];
mapNode.erase(tmpa->data);
{
}
else
{
tmpa->front->next = tmpa->next;
tmpa->next->front = tmpa->front;
}
delete tmpa;
}
}
}



posted @ 2021-03-13 22:58  卡尔的思索  阅读(22)  评论(0编辑  收藏  举报