leetcode 25.K个一组翻转链表

/*
 * @lc app=leetcode.cn id=25 lang=cpp
 *
 * [25] K 个一组翻转链表
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution
{
private:
    static ListNode *a[5000];

public:
    ListNode *&reverseKGroup(ListNode *&head, int &k)
    {

        ListNode *temp = head;
        for (int i = 0; i < k; ++i)
        {
            if (temp != nullptr)
            {
                a[i] = temp;
                temp = temp->next;
            }
            else
                return head;
        }
        head = a[k - 1];
        a[0]->next = head->next;
        for (int i = k - 1; i > 0; --i)
        {
            a[i]->next = a[i - 1];
        }
        temp = a[0];

        reverseKGroup(temp->next, k);
        return head;
    }
};
ListNode *Solution::a[];

// @lc code=end

最开始的时候使用了

static vector<ListNode*> a(k);

运行出错,折腾了半天,后来才明白leetcode的测试程序应该是在一个程序下换不同的数组跑,导致后续的数据运行时vector越界了–

leetcode 12.整数转罗马数字 无脑版代码

/*
 * @lc app=leetcode.cn id=12 lang=cpp
 *
 * [12] 整数转罗马数字
 */

// @lc code=start
#include <iostream>
using namespace std;
class Solution
{
public:
    string r;
    string intToRoman(int &num)
    {
        while (num > 0)
        {
            if (num >= 1000)
            {
                r += 'M';
                num -= 1000;
            }
            else if (num >= 900)
            {
                r += "CM";
                num -= 900;
            }
            else if (num >= 500)
            {
                r += 'D';
                num -= 500;
            }
            else if (num >= 400)
            {
                r += "CD";
                num -= 400;
            }
            else if (num >= 100)
            {
                r += 'C';
                num -= 100;
            }
            else if (num >= 90)
            {
                r += "XC";
                num -= 90;
            }
            else if (num >= 50)
            {
                r += 'L';
                num -= 50;
            }
            else if (num >= 40)
            {
                r += "XL";
                num -= 40;
            }
            else if (num >= 10)
            {
                r += 'X';
                num -= 10;
            }
            else if (num >= 9)
            {
                r += "IX";
                num -= 9;
            }
            else if (num >= 5)
            {
                r += 'V';
                num -= 5;
            }
            else if (num >= 4)
            {
                r += "IV";
                num -= 4;
            }
            else
            {
                r += 'I';
                num -= 1;
            }
        }
        return r;
    }
};
// @lc code=end

leetcode 10.正则表达式

#include <iostream>
#include <vector>
#include <functional>
using namespace std;
class Solution
{
public:
    bool isMatch(string &s, string &p)
    {
        int sSize = s.size();
        int pSize = p.size();
        vector<vector<int>> a(sSize + 1, vector(pSize + 1, 0));
        a[0][0] = 1;
        function<bool(int, int)> match = [&](int n, int m) {
            if (n < 0 || m < 0)
                return false;
            if (a[n][m] == 1)
                return true;
            if (a[n][m] == -1)
                return false;
            if (n > 0 && m > 0)
            {
                if (s[n - 1] == p[m - 1] || p[m - 1] == '.')
                    if (match(n - 1, m - 1))
                    {
                        a[n][m]=1;
                        return true;
                    }
            }
            if (m > 1)
            {
                if (p[m - 1] == '*')
                {
                    if (n > 0)
                    {
                        if (s[n - 1] == p[m - 2] || p[m - 2] == '.')
                            if (match(n - 1, m))
                                {
                        a[n][m]=1;
                        return true;
                    }
                    }
                    if (match(n, m - 2))
                        {
                        a[n][m]=1;
                        return true;
                    }
                }
            }
            a[n][m]=-1;
            return false;
        };
        return match(sSize, pSize);
    }
};