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

本文介绍了在LeetCode平台上三个与数组操作相关的编程题目:有序数组的平方使用双指针优化,长度最小子数组通过滑动窗口技巧求解,以及螺旋矩阵II的生成方法。作者分享了解题思路和关键代码段,展示了在解决这些技术问题时的策略和技巧。
 977.有序数组的平方 

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

双指针:时间复杂度O(n)

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

自己一开始没有思路的话只能想到快排,双指针的方法打开了我的视野

209.长度最小的子数组

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

滑动窗口

class Solution {
    public int minSubArrayLen(int target, int[] nums) {
        int i,j;
        int sum = 0;
        int res = 0;
        for(i = j = 0; j < nums.length; j++){
            sum += nums[j];
            while(sum >= target){
                sum -= nums[i];
                res = j-i+1;
                i++;
                
            }
        }
        return res;
    }
}

总结:

终止位置先移动,起始位置后移动。判断条件是sum>=target,长度是j-i+1,注意j-i的值,最后的落脚点时j-i是比长度小2的(跳出循环时i已经导致sum<target,且长度本身就要比j-i大1)最后还要考虑不满足条件的情况,引入result变量来解决,不能直接返回j-i+2

 59.螺旋矩阵II

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

class Solution {
    public int[][] generateMatrix(int n) {
        int[][] nums = new int[n][n];
        int startx,starty;
        int offest;
        int i,j,time;
        int count = 1;
        startx = starty = 0;
        offest = 1;
        for(time = 0; time < n/2; time++){
            i = startx;
            j = starty;
            for( ; j < n-offest; j++)
                nums[i][j] = count++;
            for( ; i < n-offest; i++)
                nums[i][j] = count++;
            for( ; j > starty; j--)
                nums[i][j] = count++;
            for ( ; i > startx; i--)
                nums[i][j] = count++;
            startx++;
            starty++;
            offest++;
        }
        if(n%2 == 1){
            nums[startx][starty] = count;
        }
    return nums;
    }
}

总结:
1.以圈为对象分析,一共是有n/2圈,不能以数的增长作为分析点。

2.每一条边的处理规则要统一。

3.单数和双数都要考虑一下。

<xarray.DataArray 'u_850' (time: 6440, lat: 73, lon: 144)> Size: 542MB array([[[ 2.830979e-05, 2.830979e-05, ..., 2.830979e-05, 2.830979e-05], [ 2.254611e+00, 2.222575e+00, ..., 2.576470e+00, 2.371954e+00], ..., [ 9.814761e-01, 1.081022e+00, ..., 8.300517e-01, 8.967269e-01], [ 2.830979e-05, 2.830979e-05, ..., 2.830979e-05, 2.830979e-05]], [[ 1.627744e-05, 1.627744e-05, ..., 1.627744e-05, 1.627744e-05], [ 9.910557e-01, 9.739634e-01, ..., 1.313991e+00, 1.103457e+00], ..., [ 8.260706e-01, 9.829472e-01, ..., 4.807461e-01, 6.483707e-01], [ 1.627744e-05, 1.627744e-05, ..., 1.627744e-05, 1.627744e-05]], ..., [[-1.529821e-04, -1.529821e-04, ..., -1.529821e-04, -1.529821e-04], [ 2.620679e-01, 1.250889e-01, ..., 5.150420e-01, 4.034959e-01], ..., [-9.225551e-01, -5.358728e-01, ..., -1.596635e+00, -1.280456e+00], [-1.529821e-04, -1.529821e-04, ..., -1.529821e-04, -1.529821e-04]], [[-2.864427e-04, -2.864427e-04, ..., -2.864427e-04, -2.864427e-04], [-7.515606e-01, -8.728150e-01, ..., -4.729214e-01, -6.136513e-01], ..., [-1.104671e+00, -7.675011e-01, ..., -1.723831e+00, -1.421816e+00], [-2.864427e-04, -2.864427e-04, ..., -2.864427e-04, -2.864427e-04]]], shape=(6440, 73, 144)) Coordinates: * lat (lat) float32 292B -90.0 -87.5 -85.0 -82.5 ... 85.0 87.5 90.0 * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5 dayofyear (time) int64 52kB ... * time (time) object 52kB '1951-06-01' '1951-06-02' ... '2020-08-31' <xarray.DataArray 'olr' (time: 2576, lat: 73, lon: 144)> Size: 217MB array([[[-6.689367, -6.689367, ..., -6.689367, -6.689367], [-6.20187 , -6.204333, ..., -6.093983, -6.11064 ], ..., [ 3.129703, 3.071567, ..., 3.336326, 3.246589], [ 5.85094 , 5.85094 , ..., 5.85094 , 5.85094 ]], [[-7.554666, -7.554666, ..., -7.554666, -7.554666], [-8.42302 , -8.405555, ..., -8.393881, -8.385301], ..., [ 3.425609, 3.291642, ..., 3.848129, 3.680576], [ 5.214539, 5.214539, ..., 5.214539, 5.214539]], ..., [[ 3.593863, 3.593863, ..., 3.593863, 3.593863], [-0.184484, -0.17038 , ..., 0.172978, 0.186692], ..., [ 0.957192, 0.927045, ..., -0.083435, -0.118446], [-4.388552, -4.388552, ..., -4.388552, -4.388552]], [[ 4.630904, 4.630904, ..., 4.630904, 4.630904], [-2.217266, -2.212579, ..., -1.174291, -1.171099], ..., [ 3.64013 , 3.61238 , ..., 2.979237, 2.946767], [-2.458136, -2.458136, ..., -2.458136, -2.458136]]], shape=(2576, 73, 144)) Coordinates: * lon (lon) float32 576B 0.0 2.5 5.0 7.5 ... 350.0 352.5 355.0 357.5 * lat (lat) float32 292B 90.0 87.5 85.0 82.5 ... -85.0 -87.5 -90.0 dayofyear (time) int64 21kB ... * time (time) object 21kB '1993-06-01' '1993-06-02' ... '2020-08-31' 以上是我的olr和u,--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[3], line 51 46 print(v_850_anomaly) 49 # 将多个变量合并为一个多变量数组 50 # multi_var = np.stack([z_anomaly1.values, z_anomaly2.values, z_anomaly3.values, z_anomaly4.values], axis=-1) ---> 51 multi_var = np.stack([olr_anomaly, u_850_anomaly, v_850_anomaly], axis=-1) 53 # 重塑数据为 (time, space*variables) 54 n_time = multi_var.shape[0] File ~/data/miniconda/envs/svd_new/lib/python3.10/site-packages/numpy/_core/shape_base.py:460, in stack(arrays, axis, out, dtype, casting) 458 shapes = {arr.shape for arr in arrays} 459 if len(shapes) != 1: --> 460 raise ValueError('all input arrays must have the same shape') 462 result_ndim = arrays[0].ndim + 1 463 axis = normalize_axis_index(axis, result_ndim) ValueError: all input arrays must have the same shape,调整olr的经纬度排序
12-18
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

林中晚霞

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

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

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

打赏作者

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

抵扣说明:

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

余额充值