0
点赞
收藏
分享

微信扫一扫

反转链表,基础栈练习,贪心

yundejia 2022-01-10 阅读 60

今天先来三道力扣题

第一题:

原题链接如下

https://leetcode-cn.com/problems/palindrome-linked-list/

 

一道链表题,方法有多种。我最开始做的方法是用数组记录链表的每个结点,将链表转换成数组,然后就用数组的方法去写。 

bool isPalindrome(struct ListNode* head){
    int a[100000],k=0;
    while(head)
    {
        a[k++] = head->val;
        head = head->next;
    }
    int i=0,j=k-1;
    while(i<j)
    {
        if(a[i]!=a[j])
        return false;
        i++;
        j--;
    }
    return true;
}

但还有空间复杂度更低的方法,直接对链表进行操作,将链表后半部分反转过来,然后进行匹配。

    while(fast)
    {
        slow = slow->next;
        fast = fast->next ? fast->next->next : fast->next;
    }

 

更改指向后使pre移动到slow,再使slow移动到temp就行了,重复这个过程到链表尾部。

 最后就是这样。

代码如下:

    while(slow)
    {
        struct ListNode* temp = slow->next;     //移动到slow的下一个结点
        slow->next = pre;           //改变slow的指向
        pre = slow;                 //上一个结点移动到当前位置
        slow = temp;                //slow移动到temp
    }

 最后只需要同时遍历头结点和pre就行了判断是否相等。

完整代码如下:

bool isPalindrome(struct ListNode* head){
    struct ListNode* slow=head,*fast=head,*pre = NULL;
    while(fast)
    {
        slow = slow->next;
        fast = fast->next ? fast->next->next : fast->next;
    }
    while(slow)
    {
        struct ListNode* temp = slow->next;     //移动到slow的下一个结点
        slow->next = pre;           //改变slow的指向
        pre = slow;                 //上一个结点移动到当前位置
        slow = temp;                //slow移动到temp
    }
    while(head && pre)
    {
        if(head->val != pre->val)
        return false;
        head = head->next;
        pre = pre->next;
    }
    return true;
}

 

第二题属于栈的题目。

https://leetcode-cn.com/problems/valid-parentheses/

括号匹配以前做过类似的题。

代码如下:

bool isValid(char * s){
    char a[10001];
    int i,len = strlen(s);
    int top = 0;
    for(i=0;i<len;i++)
    {
        if(s[i]=='('||s[i]=='['||s[i]=='{')
        a[++top] = s[i];
        else if(a[top]=='('&&s[i]==')'||a[top]=='['&&s[i]==']'||a[top]=='{'&&s[i]=='}')
        top--;
        else
        return false;
    }
    if(top==0)
    return true;
    return false;
}

第三题了!

https://leetcode-cn.com/problems/valid-parenthesis-string/

 咋一看又是一道括号匹配的题,先入为主就想用栈来写了。

参考代码入下:

bool checkValidString(char * s){
    int a[101],b[101];
    int len = strlen(s),i,top1=0,top2=0;
    for(i=0;i<len;i++)
    {
        if(s[i]=='(')
        a[++top1] = i;
        else if(s[i]=='*')
        b[++top2] = i;
        else if(s[i]==')'&&top1>0)
        top1--;
        else if(s[i]==')'&&top2>0)
        top2--;
        else
        return false;
    }
    top1=0,top2=0;
    for(i=len-1;i>=0;i--)
    {
        if(s[i]==')')
        a[++top1] = i;
        else if(s[i]=='*')
        b[++top2] = i;
        else if(s[i]=='('&&top1>0)
        top1--;
        else if(s[i]=='('&&top2>0)
        top2--;
        else
        return false;
    }
    return true;
}

其实这题还有更简单的方法,用贪心解决,

bool checkValidString(char * s){
    int len=strlen(s);
    int min=0,max=0,i;
    for(i=0;i<len;i++)
    {
        if(s[i]=='(')
        {
            min++;
            max++;
        }
        else if(s[i]==')')
        {
            min--;
            max--;
            if(min<0)
            min=0;
            if(max<0)
            return false;
        }
        else
        {
            min--;
            max++;
            if(min<0)
            min=0;
        }
    }
    if(min==0)
    return true;
    return false;
}

看到这里你也累了吧,不妨三连一波!!!

举报

相关推荐

0 条评论