代码随想录算法训练营第一天| 209.长度最小的子数组、 59.螺旋矩阵II

209 长度最小的子数组

方法一:暴力破解
方法二:滑动窗口
滑动窗口重要代码

        for (int i = 0; i < n; i++) {
            sum += nums[i];
            // 窗口滑动
            while (sum >= target) {
                ans = Math.min(ans, i - left +1);
                sum -= nums[left++];
            }
        }

方法三:前缀和

59.螺旋矩阵II

模拟法、循环遵循左闭右开的原则

    public int[][] generateMatrix(int n) {
        int[][] res = new int[n][n];

        int x = 0, y =0;

        int offset = 1;
        int count = 1;
        int loop = 1;
        int row,col;

        while (loop <= n / 2) {
        	// 从左往右
            for (col = y ; col < n - offset; col++) {
                res[x][col] = count++;
            }
			// 从上往下
            for (row = x; row < n -offset; row++) {
                res[row][col] = count++;
            }
            //从右往左
            for (;col > y;col--) {
                res[row][col] = count++;
            }
            // 从下往上
            for (;row > x;row--) {
                res[row][col] = count++;
            }
            x++;
            y++;
            loop++;
            offset++;
        }
        if (n%2 == 1) {
            res[x][y] = count;
        }
        return res;
    }
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值