数组Part02
本文最后更新于 76 天前,其中的信息可能已经有所发展或是发生改变。

977.有序数组的平方

我的写法,几乎是一次成功

class Solution
{
public:
    vector<int> sortedSquares(vector<int> &nums)
    {
        vector<int> newArray = nums;
        int right = nums.size() - 1, left = 0;
        int newLeft = newArray.size() - 1;
        while (left <= right)
        {
            if (right == left)
            {
                newArray[newLeft] = nums[right] * nums[right];
            }

            int rightSquare = nums[right] * nums[right];
            int leftSquare = nums[left] * nums[left];

            if (rightSquare > leftSquare)
            {
                newArray[newLeft] = rightSquare;
                right -= 1;
                newLeft -= 1;
            }
            else
            {
                newArray[newLeft] = leftSquare;
                left += 1;
                newLeft -= 1;
            }
        }
        return newArray;
    }
};

209.长度最小的子数组

这题使用滑动窗口法,主要的思想就是,如果sum小于target,右指针就右移扩大窗口,如果sum小于target,就把左指针向右移缩小窗口,每次符合条件的时候都会比较当前状态和length谁小,然后把小的值赋给length

class Solution
{
public:
    int minSubArrayLen(int target, vector<int> &nums)
    {
        int left = 0, right = 0;
        int length = INT32_MAX;
        int sum = 0;
        for (; right < nums.size(); right++)
        {
            sum = sum + nums[right];
            while (sum >= target)
            {
                if (right - left + 1 < length)
                {
                    length = right - left + 1;
                }
                sum = sum - nums[left++];
            }
        }

        if (length == INT32_MAX)
        {
            return 0;
        }

        return length;
    }
};

59.螺旋矩阵

由于网站上给的是左闭右开,所以我写了个左闭右闭的.不过总感觉不太优雅,不知道有没有更好的解法

class Solution
{
public:
    vector<vector<int>> generateMatrix(int n)
    {
        vector<vector<int>> res(n, vector<int>(n, 0));
        int loop = n >> 1;
        int mid = n >> 1;
        int startx = 0;
        int starty = 0;
        int offset = 0;
        int count = 1;
        int i, j;

        while (loop--)
        {
            i = startx;
            j = starty;

            
            for (j; j < n - offset; j++)
            {
                res[i][j] = count++;
            }
            j--;//解决由循环导致的溢出
            i++;//跳转到下一行
           
            for (i; i < n - offset; i++)
            {
                res[i][j] = count++;
            }
            i--;
            j--;
           
            for (; j >= starty; j--)
            {
                res[i][j] = count++;
            }
            i--;
            j++;
           
            for (; i > startx; i--) //由于startx和最后一条边的闭区间重叠,所以不能用
            {
                res[i][j] = count++;
            }

            
            startx++;
            starty++;
            offset += 1;
        }

        if (n % 2 != 0)
        {
            res[mid][mid] = count;
        }

        return res;
    }
};

oh!no,上厕所的时候才发现了我写法的奇怪之处!不知道大家有没有发现,这其实不是左闭右闭,而是~~“左闭右闭左开右闭左开右开”~~的写法.也就是说我的第一条边是左闭右闭,第二,三条边是左开右闭,第四条边是左开右开(()).事已至此,明天再重写一遍吧().

评论

  1. Coke
    2 月前
    2024-7-07 13:06:27

    23333

  2. Coke
    2 月前
    2024-7-07 13:04:02

    我是小猫

  3. Delusion
    2 月前
    2024-7-07 12:53:11

    hi233

  4. Delusion
    2 月前
    2024-7-07 12:48:30

    hi

  5. lazyy
    2 月前
    2024-7-07 12:39:42

    给我来杯水

    • ForsakenDelusion
      lazyy
      2 月前
      2024-7-07 12:41:42

      给你来杯卡布奇诺

  6. lazyy
    2 月前
    2024-7-07 2:42:27

    好用

    • ForsakenDelusion
      lazyy
      2 月前
      2024-7-07 12:36:39

      再发一条

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇