• 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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    在这里插入图片描述

    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++;
            }
        }
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    在这里插入图片描述

    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;
    	}
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

    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);
        }
    };
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    在这里插入图片描述

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    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;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在这里插入图片描述

  • 相关阅读:
    使用HttpServlet开发web应用
    redis基于Stream类型实现消息队列,命令操作,术语概念,个人总结等
    可视化设计:一文读懂桑基图,从来处来,到去出去。
    电动力学专题研讨:运动电荷之间的相互作用是否满足牛顿第三定律?
    docker&Kubernetes入门篇(二)
    python单例模式应用之pymongo连接
    微信小程序 checkbox 实现双向绑定以及特殊交互处理
    C语言最佳实践(B站学习)更新
    10、MyBatis-Plus 多数据源
    Redisson 3.17.4 发布
  • 原文地址:https://blog.csdn.net/Edward_LF/article/details/125897972