算法第一天(基于某某随想录)

第一章 数组part01

今日任务

数组理论基础,704. 二分查找,27. 移除元素

详细布置
数组理论基础

文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html

题目建议: 了解一下数组基础,以及数组的内存空间地址,数组也没那么简单。

704. 二分查找

题目建议: 大家能把 704 掌握就可以,35.搜索插入位置 和 34. 在排序数组中查找元素的第一个和最后一个位置 ,如果有时间就去看一下,没时间可以先不看,二刷的时候在看。

先把 704写熟练,要熟悉 根据 左闭右开,左闭右闭 两种区间规则 写出来的二分法

题目链接:https://leetcode.cn/problems/binary-search/

没啥可说的,基础课模版

c++:

class Solution {
public:
    int search(vector<int>& nums, int target) {
        int l = 0,r = nums.size() - 1;
        while(l < r)
        {
            int mid = l + (r - l) / 2;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        } 
        if(nums[r] != target) return -1;
        return r;
    }
};

java:

class Solution {
    public int search(int[] nums, int target) {
        int l = 0,r = nums.length - 1;
        while(l < r)
        {
            int mid = l + (r - l) / 2;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        if(nums[r] != target) return -1;
        return r;

    }
}

文章讲解:https://programmercarl.com/0704.%E4%BA%8C%E5%88%86%E6%9F%A5%E6%89%BE.html

视频讲解:https://www.bilibili.com/video/BV1fA4y1o715

27. 移除元素

题目建议: 暴力的解法,可以锻炼一下我们的代码实现能力,建议先把暴力写法写一遍。 双指针法 是本题的精髓,今日需要掌握,至于拓展题目可以先不看。

题目链接:https://leetcode.cn/problems/remove-element/

通用解法

本解法的思路与 【题解】26. 删除排序数组中的重复项 中的「通用解法」类似。

先设定变量 idx,指向待插入位置。idx 初始值为 0

然后从题目的「要求/保留逻辑」出发,来决定当遍历到任意元素 x 时,应该做何种决策:

  • 如果当前元素 x 与移除元素 val 相同,那么跳过该元素。
  • 如果当前元素 x 与移除元素 val 不同,那么我们将其放到下标 idx 的位置,并让 idx 自增右移。

最终得到的 idx 即是答案。

c++:

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int idx = 0;
        for(int x : nums)
        {
            if(x != val) nums[idx++] = x;
        }
        return idx;

    }
};

java:

class Solution {
    public int removeElement(int[] nums, int val) {
        int idx = 0;
        for(int x : nums)
        {
            if(x != val) nums[idx++] = x;
        }
        return idx;
        
    }
}

文章讲解:https://programmercarl.com/0027.%E7%A7%BB%E9%99%A4%E5%85%83%E7%B4%A0.html

视频讲解:https://www.bilibili.com/video/BV12A4y1Z7LP

第一章 数组part02

今日任务

977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II ,总结

建议大家先独立做题,然后看视频讲解,然后看文章讲解,然后在重新做一遍题,把题目AC,最后整理成今日当天的博客

拓展题目可以先不做

详细布置

977.有序数组的平方

题目建议: 本题关键在于理解双指针思想

题目链接:https://leetcode.cn/problems/squares-of-a-sorted-array/

#双指针法

数组其实是有序的, 只不过负数平方之后可能成为最大数了。

那么数组平方的最大值就在数组的两端,不是最左边就是最右边,不可能是中间。

此时可以考虑双指针法了,i指向起始位置,j指向终止位置。

定义一个新数组result,和A数组一样的大小,让k指向result数组终止位置。

如果A[i] * A[i] < A[j] * A[j] 那么result[k--] = A[j] * A[j];

如果A[i] * A[i] >= A[j] * A[j] 那么result[k--] = A[i] * A[i];

c++:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
        int n = nums.size() - 1;
        vector<int> res(nums.size());
        for(int i = 0,j = nums.size() - 1;i <= j;)
        {
            if(nums[i] * nums[i] >= nums[j] * nums[j])
            {
                res[n--] = nums[i] * nums[i];
                i++;
            } 
            else
            {
                res[n--] = nums[j] * nums[j];
                j--;
            }
        }
        return res;
    }
};

java:

class Solution {
    public int[] sortedSquares(int[] nums) {
        int n = nums.length - 1;
       int[] res = new int[nums.length];
       for(int i = 0,j = nums.length - 1;i <= j;)
       {
        if(nums[i] * nums[i] >= nums[j] * nums[j])
        {
            res[n--] = nums[i] * nums[i];
            i++;
        }else
        {
             res[n--] = nums[j] * nums[j];
             j--;
        }
       }
       return res;

    }
}

文章讲解:https://programmercarl.com/0977.%E6%9C%89%E5%BA%8F%E6%95%B0%E7%BB%84%E7%9A%84%E5%B9%B3%E6%96%B9.html

视频讲解: https://www.bilibili.com/video/BV1QB4y1D7ep

209.长度最小的子数组

题目建议: 本题关键在于理解滑动窗口,这个滑动窗口看文字讲解 还挺难理解的,建议大家先看视频讲解。 拓展题目可以先不做。

题目链接:https://leetcode.cn/problems/minimum-size-subarray-sum/

常规的滑动窗口模板题

c++:

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int sum = 0;
        int res = INT32_MAX;
        //j 为起始端点 i为末尾端点 
        for(int i = 0,j = 0;j < nums.size();j++){
            sum += nums[j];//右窗口
            while(sum >= target){
                res = min(res,j - i + 1);
                sum -= nums[i++];//收缩左窗口
            }
        }
        if(res == INT32_MAX) return 0;
        else return res;
    }
};

java:

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int res = Integer.MAX_VALUE;
        int sum = 0;
        //j 为起始端点 i为末尾端点 
        for(int i = 0,j = 0;i < nums.length;i++)
        {
            sum += nums[i];
            while(sum >= target)
            {
                res = Math.min(res,i - j + 1);
                sum -= nums[j++];
            }
        }
        if(res != Integer.MAX_VALUE) return res;
        else return 0;
    }
}

文章讲解:https://programmercarl.com/0209.%E9%95%BF%E5%BA%A6%E6%9C%80%E5%B0%8F%E7%9A%84%E5%AD%90%E6%95%B0%E7%BB%84.html

视频讲解:https://www.bilibili.com/video/BV1tZ4y1q7XE

59.螺旋矩阵II

题目建议: 本题关键还是在转圈的逻辑,在二分搜索中提到的区间定义,在这里又用上了。

题目链接:https://leetcode.cn/problems/spiral-matrix-ii/

偏移量写法

c++:

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> res(n,vector<int>(n,0));
        //偏移量
        int dx[4] = {-1,0,1,0};
        int dy[4] = {0,1,0,-1};
        //初始状态 
        int x = 0,y = 0,d = 1;
        for(int i = 1;i <= n * n;i++)
        {
            res[x][y] = i;
            int a = x + dx[d],b = y + dy[d];
            //出界或者走过重复的路时才进行螺旋(转弯)
            if(a < 0 || a >= n || b < 0 || b >= n || res[a][b])
            {
                d = (d + 1) % 4;
                a = x + dx[d],b = y + dy[d];
            }
            x = a,y = b;
        }
        return res;
    }
};

常规写法

解题思路:

初始化一个 n×n 大小的矩阵 mat,然后模拟整个向内环绕的填入过程:

  1. 定义当前左右上下边界 l,r,t,b,初始值 num = 1,迭代终止值 tar = n * n

  2. num <= tar时,始终按照
    
    从左到右 从上到下 从右到左 从下到上
    

    填入顺序循环,每次填入后:

    1. 执行 num += 1:得到下一个需要填入的数字;
    2. 更新边界:例如从左到右填完后,上边界 t += 1,相当于上边界向内缩 1。
  3. 使用num <= tar而不是l < r || t < b作为迭代条件,是为了解决当n为奇数时,矩阵中心数字无法在迭代过程中被填充的问题。

  4. 最终返回 mat 即可。

java:

class Solution {
    public int[][] generateMatrix(int n) {
        int l = 0,r = n - 1,t = 0,b = n - 1;
        int[][] mat = new int[n][n];
        int num = 1,tar = n * n;
        while(num <= tar)
        {
            for(int i = l;i <= r;i++) mat[t][i] = num++; // left to right.
            t++;
            for(int i = t;i <= b;i++) mat[i][r] = num++;// top to bottom.
            r--;
            for(int i = r;i >= l;i--) mat[b][i] = num++; // right to left.
            b--;
            for(int i = b;i >= t;i--)mat[i][l] = num++; // bottom to top.
            l++;
        }
        return mat;
    }
}

文章讲解:https://programmercarl.com/0059.%E8%9E%BA%E6%97%8B%E7%9F%A9%E9%98%B5II.html

视频讲解:https://www.bilibili.com/video/BV1SL4y1N7mV/

总结

题目建议:希望大家 也做一个自己 对数组专题的总结

文章链接:https://programmercarl.com/%E6%95%B0%E7%BB%84%E6%80%BB%E7%BB%93%E7%AF%87.html-

  • 18
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值