471-82(647、5、92、143、148、19)

647. 回文子串

在这里插入图片描述

class Solution {
public:
    int countSubstrings(string s) {

        vector<vector<bool>> dp(s.size(), vector<bool>(s.size(), false));

        int res = 0;

        for (int i = s.size() - 1; i >= 0; i--)
        {
            for (int j = i; j < s.size(); j++)
            {
                if (s[i] == s[j])
                {
                    if (j - i <= 1)
                    {
                        res++;
                        dp[i][j] = true;
                    }
                    else if (dp[i + 1]dp[j - 1])
                    {
                        res++;
                        dp[i][j] = true;
                    }
                }
            }
        }
        return res;
    }
};

在这里插入图片描述

5. 最长回文子串

在这里插入图片描述

//中心扩散法
class Solution {
public:
    vector<string> res;
public:
    string longestPalindrome(string s) {
        for (int i = 0; i < s.size(); i++)
        {
            extend(s, i, i);
            extend(s, i, i + 1);
        }

        string temp;

        for (int i = 0; i < res.size(); i++)
        {
            if (res[i].size() > temp.size())
            {
                temp = res[i];
            }
        }

        return temp;
    }

    void extend(string s, int i, int j)
    {
        while (i >= 0 && j < s.size() && s[i] == s[j])
        {
            res.push_back(string(s.begin() + i, s.begin() + j + 1));
            i--;
            j++;
        }
    }
};

在这里插入图片描述

92. 反转链表 II

在这里插入图片描述

class Solution
{
public:
	ListNode* reverseBetween(ListNode* head, int left, int right)
	{

		vector<int> vec;

		ListNode* cur = head;
		while (cur != nullptr)
		{
			vec.emplace_back(cur->val);
			cur = cur->next;
		}

		reverse(vec.begin() + left - 1, vec.begin() + right);

        ListNode* dummy_node = new ListNode(0);
        ListNode* temp = dummy_node;

		for (int i = 0; i < vec.size(); i++)
		{
			temp->next = new ListNode(vec[i]);
			temp = temp->next;
		}

		return dummy_node->next;
	}
};

在这里插入图片描述

143. 重排链表

在这里插入图片描述




class Solution {

    //用双指针找链表的mid中间节点
public:
    ListNode* middNode(ListNode* head)
    {
        ListNode* slow = head;
        ListNode* fast = head;

        while (fast->next != nullptr && fast->next->next != nullptr)
        {
            slow = slow->next;
            fast = fast->next->next;
        }
        return slow;
    }

    //反转链表
    ListNode* reverseList(ListNode* head)
    {
        ListNode* pre = nullptr;
        ListNode* cur = head;

        while (cur != nullptr)
        {
            ListNode* temp = cur->next;
            cur->next = pre;
            pre = cur;
            cur = temp;
        }
        return pre;
    }

    void mergeList(ListNode* l1, ListNode* l2)
    {
        ListNode* l1_temp;
        ListNode* l2_temp;

        while (l1 != nullptr && l2 != nullptr)
        {
            l1_temp = l1->next;
            l2_temp = l2->next;

            l1->next = l2;
            l1 = l1_temp;

            l2->next = l1;
            l2 = l2_temp;
        }
    }

public:
    void reorderList(ListNode* head) {
        if (head == nullptr)
        {
            return;
        }

        ListNode* mid = middNode(head);
        
        ListNode* l1 = head;
        ListNode* l2 = mid->next;
        mid->next = nullptr;
        l2 = reverseList(l2);

        mergeList(l1, l2);
    }
};


在这里插入图片描述

148. 排序链表

在这里插入图片描述

class Solution {
public:
    ListNode* sortList(ListNode* head) {

        vector<int> vec;

        ListNode* cur = head;

        while (cur != nullptr)
        {
            vec.emplace_back(cur->val);
            cur = cur->next;
        }

        sort(vec.begin(), vec.end());

        ListNode* dummy_node = new ListNode(0);
        ListNode* temp = dummy_node;

        for (int i = 0; i < vec.size(); i++)
        {
            temp->next = new ListNode(vec[i]);
            temp = temp->next;
        }
        return dummy_node->next;
    }
};

在这里插入图片描述

19. 删除链表的倒数第 N 个结点

在这里插入图片描述

class Solution {
public:
    ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode* cur = head;

        int n1 = 0; //记录链表总长度
        while (cur != nullptr)
        {
            n1++;
            cur = cur->next;
        }
        if(n1 == 1) return nullptr;

        int m = n1 - n + 1;

        cur = head;
        ListNode* dummy = new ListNode(0);
        dummy->next = head;
        ListNode* pre = dummy;
        m--;
        while (m-- && cur != nullptr)
        {
            pre = cur;
            cur = cur->next;
        }
        pre->next = cur->next;
        return dummy->next;
    }
};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

liufeng2023

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值