C++题目

如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)

typedef struct node{
    int ele;
    struct node * next;
}node;
bool check( node *head)
{
    node *low=head;
    node *fast=head;
    if(head==NULL)
        return false;
    while(fast->next!=NULL&&fast!=NULL)
    {
        if(fast->ele==low->ele)
            return true;
        else
        {
            fast=fast->next->next;
            low=low->next;
        }
    }
    return false;
}

写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数

普通算法:

 for (i=0; T[i] != '\0'; i++)
    {
    for (j=0; T[i+j] != '\0' && P[j] != '\0' && T[i+j]==P[j]; j++) ;
    if (P[j] == '\0') found a match
    }

KMP算法:

 

posted @ 2013-04-13 10:57  小叫花子  阅读(189)  评论(0编辑  收藏  举报