代码随想录算法训练营第二天 | LeetCode977有序数组的平方, LC209长度最小的子数组, LC59螺旋矩阵II

LeetCode977

文章链接:LeetCode977
视频链接:LeetCode977
代码:

class Solution {
public:
    vector<int> sortedSquares(vector<int>& nums) {
       int i = 0, j = nums.size()-1;
       vector<int> result;
       while (i <= j) {
           if (abs(nums[i]) > abs(nums[j])) {
               result.push_back(nums[i] * nums[i]);
               i++;
           }
           else {
               result.push_back(nums[j] * nums[j]);
               j--;
           }
       }
        reverse(result.begin(), result.end());
        return result;
    }
};

LeetCode209长度最小的子数组

文章链接:LC209
视频链接:LC209
代码:(没写出来)

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int i = 0, j = 0;
        int min_length = ;
        for (; i < j, i<nums.size(), j<nums.size(); ) {
            int sum = 0;
            // 遍历一遍快指针下标到慢指针下标
            for (k = i; k<=j ;k++) {
                sum += nums[k];
            }
            if (sum>=target) min_length = j - i + 1;
            else j++;
        }
        return min_length;
    }
};

LeetCode59螺旋矩阵

文章链接:LC59
视频链接:LC59
代码:(没写出来)

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> result(n, vector<int> (n, 0));
        int i = 0; int j = 0;      // i为行号 j为列号
        num = 1;

        // for ( ;j < n; j++) {       // 遍历第一行
        //     result[i][j] = num;
        //     num++;
        // } 
        // for ( ;i < n; i++) {        // 遍历最后一列
        //     result[i][j] = num;
        //     num++;
        // }

        for ( ;i < n, j < n; num++) {
            if (i==0 && j==0) {
                result[i][j] = num;
                j++;
            }
            else if (j==n-1 && i==0) {
                result[i][j] = num;
                i++;
            }
            else if {i==n-1} {
                result[i][j] = num;
            }
            else if
        } 
    }
};

摆了太久复检效率还是不太高,拖延症加上最近毕业论文有点烦人,今天时间不够,题也没做出来。明天回来补!!!


lc59 螺旋矩阵II (3.10)

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        vector<vector<int>> result(n, vector<int> (n, 0));
        int startx = 0, starty = 0;
        int count = 1;
        int offset = 1;
        int i, j;
        int loop = n / 2;            // 转圈的圈数 n=4 时,需要转2圈
        while (loop--) {
            for (i = startx, j=starty; j < n-offset; j++) {         // 当n=4时转第1圈,j从0变到2(<4-offset=3)
                result[i][j] = count;
                count++;
            }
            for ( ; i < n-offset; i++) {
                result[i][j] = count;
                count++;
            }
            for ( ; j > starty; j--) {
                result[i][j] = count;
                count++;
            }
            for ( ; i > startx ; i--) {
                result[i][j] = count;
                count++;
            }
            startx++;
            starty++;
            offset++;
        }
        if (n%2 == 1) {
            result[startx][starty] = count;
        }
        return result;
    }
};

收获:循环的次数等于转的圈数(n=4转两圈,n=6转3圈)
每一圈要更改转圈开始的下标还有终止的下标,所以要设置startx, starty, offset这些变量
n为奇数的时候要把最后一“圈”单独拎出来赋值

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值