有序数组的平方、最小连续子数组、螺旋数组

给你一个按 非递减顺序 排序的整数数组 nums,返回 每个数字的平方 组成的新数组,要求也按 非递减顺序 排序。

来源:力扣(LeetCode)
link

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

给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。

来源:力扣(LeetCode)
link

class Solution {
public:
    int minSubArrayLen(int target, vector<int>& nums) {
        int i = 0, j = 0;
        int result = INT_MAX, sum = 0;
        for(j = 0; j < nums.size(); j++){
            sum += nums[j];
            while(sum >= target){
                result = result > j - i + 1?j - i + 1:result;
                sum -= nums[i++];
            }
        }
        return result == INT_MAX?0:result;
    }
};

给你一个正整数 n ,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的 n x n 正方形矩阵 matrix 。

来源:力扣(LeetCode)
link

class Solution {
public:
   vector<vector<int>> generateMatrix(int n) {
       vector<vector<int>> a(n, vector<int>(n, 0));
       int startx = 0, offset = 1, starty = 0;
       int i = startx, j = starty, count = 1;
       int nes = n/2;
       int mid = n/2;
       while(nes--){
           for(j = starty; j < n-offset; j++) a[startx][j] = count++;
           for(i = startx; i < n - offset; i++)  a[i][j] = count++;
           for(; j > starty; j--) a[i][j] = count++;
           for(; i > startx; i--) a[i][j] = count++;
           startx++;
           starty++;
           offset++;
       }
       if(n%2 == 1) a[mid][mid] = count;
       return a;
   }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

繁华的梦境

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

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

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

打赏作者

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

抵扣说明:

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

余额充值