LC刷题第一天

招商银行信用卡

21. 合并两个有序链表

直接模拟一遍即可,不需要新建结点,直接修改每个结点指向即可

/**
 * 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 {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* head = new ListNode(-1);
        if (!list1 && !list2) return nullptr;
        head -> next = nullptr;
        auto cur = head;
        while (list1 && list2) {
            if (list1->val <= list2->val) {
                cur->next=list1;
                cur = cur->next;
                list1=list1->next;
            }
            else {
                cur->next=list2;
                cur = cur->next;
                list2=list2->next;
            }
        }
        if (list1) cur->next = list1;
        if (list2) cur->next = list2;
        return head->next;
    }
};

3. 无重复字符的最长子串

开一个数组或者哈希存储滑动窗口中字母出现的次数

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        map<char, int> mp;
        int res = 0;
        for (int i = 0, j = 0; i < s.size(); i ++ ) {
            mp[s[i]] ++ ;
            while (mp[s[i]] > 1) -- mp[s[j ++ ]];
            res = max(res, i - j + 1);
        }
        return res;
    }
};

322. 零钱兑换

f[i][j]表示考虑前i种货币,凑成j金额需要的张数最小值,递推就是第 i i i种货币使用几张,通过f[i][j-w]优化的一道完全背包问题变形

class Solution {
public:
    int coinChange(vector<int>& coins, int amount) {
        int n = coins.size();
        vector<int> f(amount + 1, 0x3f3f3f3f);
        f[0] = 0;
        for (int i = 0; i < n; i ++ )
        {
            int w = coins[i];
            for (int j = w; j <= amount; j ++ )
                f[j] = min(f[j], f[j - w] + 1);
        }
        if (f[amount] == 0x3f3f3f3f) return -1;
        return f[amount];
    }
};

编程能力

896. 单调数列

记录升和降是否存在,如果同时升降存在返回false

class Solution {
public:
    bool isMonotonic(vector<int>& nums) {
        int n = nums.size();
        if (n <= 2) return true;

        bool up = false, down = false;
        for (int i = 0; i < n - 1; i ++ )
            if (nums[i + 1] > nums[i]) up = true;
            else if (nums[i + 1] < nums[i]) down = true;
        return !(up && down);
    }
};

28. 实现 strStr()

首先考虑边界,如果 n e e d l e needle needle空返回0,如果 h a y s t a c k haystack haystack为空返回-1,调用substr函数判断是否包含 n e e d l e needle needle

class Solution {
public:
    int strStr(string haystack, string needle) {
        int n = haystack.size(), m = needle.size();
        if (!m) return 0;
        else if (!n) return -1;

        for (int i = 0; i + m - 1 < n; i ++ )
            if (haystack.substr(i, m) == needle)
                return i;
        return -1;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Shirandexiaowo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值