2020年12月4日leetcode每日一題

weixin_49434128發表於2020-12-04

思路
抄的leetcode,今天比較忙,明天補上
程式碼

struct dilinklist
{
    int endval;
    int len;
    struct dilinklist *prenode;
    struct dilinklist *nextnode;
}listnode;

int Addlen(struct dilinklist *p , int key , int keynum)
    {
        while(p->prenode != NULL && p->endval == key - 1 && keynum > 0)
        {
            p->endval = key;
            ++p->len;
            keynum--;
            p = p->prenode;
        }
        return keynum;
    }
struct dilinklist *setnewnode(struct dilinklist *p , int key , int keynum)
    {
        while(keynum > 0)
        {
            struct dilinklist *s = (struct dilinklist*)malloc(sizeof(struct dilinklist));
            s->endval = key;
            s->len = 1;
            s->prenode = p;
            p->nextnode = s;
            s->nextnode = NULL;
            p = s;
            keynum--;
        }
        return p;
    }

bool isPossible(int* nums, int numsSize){
    if(numsSize < 3)    return false;
    struct dilinklist *head = (struct dilinklist*)malloc(sizeof(struct dilinklist));
    head->prenode = NULL;
    head->endval = __WINT_MIN__;
    head->len = 3; 
    struct dilinklist *p = (struct dilinklist*)malloc(sizeof(struct dilinklist));//p始終指向表的結尾
    p->endval = nums[0];
    p->len = 1;
    p->prenode = head;
    head->nextnode = p;
    p->nextnode = NULL;
    for(int i = 1 ; i < numsSize ;++i)
    {
        int count = 1;
        int x = nums[i];
        for(;i < numsSize - 1 && nums[i+1] == nums[i] ; ++i)    
            ++count;
        if((count = Addlen(p , x , count)))//將count個數值為x的數字併到已有的子序列中,返回多餘的數字
            p = setnewnode(p , x , count);//根據多餘的數字建立新的子序列;
    }
    //遍歷表,如果有len<3的情況,輸出false
    while(p->prenode != NULL)
    {
        if(p->len < 3)  return false;
        //printf("%d %d \n" , p->endval,p->len);
        p = p->prenode;
    }
    return true;
}

相關文章