代码随想录训练营第二天|lc209长度最小的子数组, 59螺旋矩阵II,lc203移除链表元素,lc206反转链表

LC 209 长度最小的子数组

209. 长度最小的子数组 - 力扣(LeetCode)

1.n^2暴力解,两层for,会超时
class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int res = INT_MAX;
        for (int i = 0; i < nums.size(); i++){
            int sum = 0;
            for (int j = i; j < nums.size(); j++){
                sum += nums[j];
                if (sum >= target) {
                    res = min(res, (j - i + 1));
                    break;
                }
            }
        }
        if (res == INT_MAX) return 0;
        else return res;
    }
};
2.滑动窗口O(n)

滑窗对窗口起始和末尾的调整还是很有意思的。

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        //滑窗
        //start表示窗口起始位置, end表示窗口末尾
        
        int res = INT_MAX;
        int start = 0;
        int sum = 0;
        for (int end = 0; end < nums.size(); end++) {
            sum += nums[end];
            while (sum >= target){
                res = min (res, end - start + 1);

                //如果窗口值已经大于目标,则可以滑动窗口起始位置,直至窗口值小于目标,再移动窗口末尾
                sum -= nums[start];
                start++;
            }
        }
        if (res == INT_MAX) return 0;
        else return res;
    }
};

LC 59 螺旋矩阵II

这题不涉及算法,主要是对代码的操控力

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n, vector<int>(n, 0));
        //dx,dy 用于记录打印方向 direction_x; direction_y 此处简写
        int dx = 0;
        int dy = 1;
        //px, py用于记录打印位置 position_x; position_y 此处简写
        int px = 0;
        int py = 0;
        for (int i = 1; i <= n * n; i++) {
            res[px][py] = i;
            //此处判断是否应该调转方向,对n取模是为了防止数组越界,加上一个n也是防止越界,因为dy = -1时,y + dy可能小于0
            if (res[(px + dx + n) % n][(py + dy + n) % n] != 0) {
                //90度调转反向
                int temp = dy;
                dy = -dx;
                dx = temp;
            }
            px += dx;
            py += dy;
        }
        return res;
    }
};

LC 203 移除链表元素

重拾一下 链表这种数据结构

/**
 * 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* removeElements(ListNode* head, int val) {
        while (head != nullptr && head->val == val) {
            //头节点后移一位,然后直接删除原本头节点
            ListNode* temp = head;
            head = head->next;
            delete temp;
        }
        ListNode *temp = head;
        while (temp != nullptr && temp->next != nullptr) {
            //防止越界
            if (temp->next->val == val) {
                ListNode *t = temp->next;
                temp->next = temp->next->next;
                delete t;
            } else temp = temp->next;
        }
        return head;
    }
};
LC 206 反转链表

主要就在于理解链表这种数据结构

/**
 * 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* reverseList(ListNode* head) {
        //思路:不断调转链表元素指向
        ListNode *temp;
        ListNode *r = head;
        ListNode *l = nullptr;
        while (r != nullptr) {
            temp = r->next;
            r->next = l;
            l = r;
            r = temp;
        }
        return l;
    }
};

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值