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

文章介绍了在LeetCode平台上使用C#编程语言解决的三道问题:有序数组平方的排序、长度最小的子数组找到满足条件的子数组长度,以及螺旋矩阵的生成。主要展示了双指针法的应用和解题思路。
摘要由CSDN通过智能技术生成

977.有序数组的平方

977. 有序数组的平方 - 力扣(LeetCode)

思路:直接用双指针法解决,把平方后的元素再替换进数组中,之后利用数组sort排序再输出数组

C#代码:

public class Solution {
    public int[] SortedSquares(int[] nums) {
        int m = 0;
        for(int k = 0;k < nums.Length;k++){
            nums[m] = nums[k]*nums[k];
            m++;
        }
        Array.Sort(nums);
        return nums;
    }
}

209.长度最小的子数组

209. 长度最小的子数组 - 力扣(LeetCode)

思路:双指针的方法,快指针循环直到sum>=target,再对快慢指针包括的数组的长度比较,比较后减去慢指针i所属的元素,再i++。结束一遍循环后再判断sum是否大于等于target,是就继续减去慢指针,再i++,否则再for循环一遍,快指针往后移动,直到sum>=target或者到达数组的长度。所有循环完成后,判断一遍最小长度的值后就输出。

C#代码:

public class Solution {
    public int MinSubArrayLen(int target, int[] nums) {
        int i=0,miniLen = int.MaxValue;
        int sum = 0;
        for(int j=0;j<nums.Length;j++){
            sum += nums[j];
            while(sum >= target){
                miniLen = Math.Min(j-i+1,miniLen);
                sum -= nums[i];
                i++;
            }
        }
        return miniLen == int.MaxValue?0:miniLen;
    }
}

59.螺旋矩阵II

59. 螺旋矩阵 II - 力扣(LeetCode)

思路:只要能明白螺旋过程出现拐角前一直保持行(列)不变,拐弯后就保持列(行)不变就能解决。还有注意用的方法是二分法中的左闭右开的方法。(左闭右闭还没研究怎么实现,但左闭右开的方法应该比较容易理解和写出的)

C#代码:

public class Solution {
    public int[][] GenerateMatrix(int n) {
        int[][] a = new int[n][];
        for(int i=0;i<n;i++){
            a[i] = new int[n];
        }
        int count =1;
        int x = n-1,y = 0;
        while(count < n*n){
            for(int i=y;i<x;i++){a[y][i] = count++;}
            for(int i=y;i<x;i++){a[i][x] = count++;}
            for(int i=x;i>y;i--){a[x][i] = count++;}
            for(int i=x;i>y;i--){a[i][y] = count++;}
            y++;
            x--;
        }
        if(n%2==1){a[n/2][n/2] = count;}
        return a;
        
    }
}

54. 螺旋矩阵 - 力扣(LeetCode)

思路:

public class Solution {
    public IList<int> SpiralOrder(int[][] matrix) {
            IList<int> order = new List<int>();
            int X = matrix.Length;
            int Y = matrix[0].Length;
            int sum = X * Y;
            int count = 1;
            //上边界、下边界、左边界、右边界
            int top = 0,bottom = X - 1, left = 0,right = Y - 1;
            while (0 < sum)
            {
                for(int col = left; col <= right && sum > 0;col++){
                    order.Add(matrix[top][col]);
                    sum--;
                }
                top++;
                for(int row = top; row <= bottom && sum > 0; row++){
                    order.Add(matrix[row][right]);
                    sum--;
                }
                right--;
                for(int col = right;col >= left && sum > 0;col--){
                    order.Add(matrix[bottom][col]);
                    sum--;
                }
                bottom--;
                for(int row = bottom;row >= top && sum > 0;row--){
                    order.Add(matrix[row][left]);
                    sum--;
                }
                left++;
            }
            return order;
    }
}

总结:前两道题看一下视频知道是用双指针后很容易就解决,不过这第三道螺旋矩阵看视频一时间没想起来,后面多画几张图之后才明白是怎样的一个思路。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值