2021年3月刷题(下) Spiral Matrix,Merge Interval

题目都来源于leetcode网站。题目的图片来自或参考原网站绘制。

54. Spiral Matrix
Given an m x n matrix, return all elements of the matrix in spiral order.

Example 1:
来自leetcode网站
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [1,2,3,6,9,8,7,4,5]
输入为一个m x n的数组,从位置[0,0]开始按照顺时针方向螺旋型遍历数组,输出遍历的结果。
解题的关键是遍历方向的改变,最开始的代码设计只考虑了方向的变化,提交的时候很多测试用例都不能通过。过了一段时间突然发现:方向的变化通过边界判断可以实现,遍历什么时候结束只需要计算遍历的总数即可。
提交的代码如下:

public class Solution {
    public IList<int> SpiralOrder(int[][] matrix) {
    	List<int> result = new List<int>();
        if (matrix.Length == 0) return result;
        int level = 0;
        int toward = 1;
        int cur_x=0, cur_y = 0;
        result.Add(matrix[cur_x][cur_y]);
        while (result.Count < matrix.Length * matrix[0].Length) 
        {
            switch (toward) 
            {
                case 1:
                    if (cur_y + 1 < (matrix[0].Length - level))
                    {
                        result.Add(matrix[cur_x][++cur_y]);
                    }
                    else 
                    {
                        toward++;
                    }
                    break;
                case 2:
                    if (cur_x + 1 < (matrix.Length - level))
                    {
                        result.Add(matrix[++cur_x][cur_y]);
                    }
                    else
                    {
                        toward++;
                    }
                    break;
                case 3:
                    if (cur_y - 1 >= level)
                    {
                        result.Add(matrix[cur_x][--cur_y]);
                    }
                    else
                    {
                        toward++;
                    }
                    break;
                case 4:
                    if (cur_x - 1 > level)
                    {
                        result.Add(matrix[--cur_x][cur_y]);
                    }
                    else
                    {
                        toward=1;
                        level++;
                    }
                    break;
            }
        }
        return result;
    }
}

1-4分别代表4个方向,1表示向右,2表示向左,3表示向后,4表示向上。
在这里插入图片描述
相关的题目还有两个。
59. Spiral Matrix II
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n x n in spiral order.
来自leetcode
Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
输入正整数n,输出 n x n 数组,数组用 1到 n x n 的数填充,规则是从位置[0,0] 开始顺时针螺旋形。
这题和上一题的思路基本类似,第一题是按规则读数组值,这一题是按规则填充数组值。

public class Solution {
    public int[][] GenerateMatrix(int n) {
        if (n == 0) return null;
            int[][] result = new int[n][];
            for (int i = 0; i < n; i++)
            {
                result[i] = new int[n];
            }
            int cur_num = 1;
            int cur_x = 0, cur_y = 0;
            result[cur_x][cur_y] = cur_num;
            int level = 0;
            int toward = 1;
            while (cur_num < n * n) 
            {
                switch (toward) 
                {
                    case 1:
                        if (cur_y + 1 < (n - level))
                        {
                            result[cur_x][++cur_y]=++cur_num;
                        }
                        else
                        {
                            toward++;
                        }
                        break;
                    case 2:
                        if (cur_x + 1 < (n - level))
                        {
                            result[++cur_x][cur_y]=++cur_num;
                        }
                        else
                        {
                            toward++;
                        }
                        break;
                    case 3:
                        if (cur_y - 1 >= level)
                        {
                            result[cur_x][--cur_y]=++cur_num;
                        }
                        else
                        {
                            toward++;
                        }
                        break;
                    case 4:
                        if (cur_x - 1 > level)
                        {
                            result[--cur_x][cur_y]=++cur_num;
                        }
                        else
                        {
                            toward = 1;
                            level++;
                        }
                        break;
                }
            }
            return result;
    }
}

885. Spiral Matrix III
On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east.
Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.
Now, we walk in a clockwise spiral shape to visit every position in this grid.
Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)
Eventually, we reach all R * C spaces of the grid.
Return a list of coordinates representing the positions of the grid in the order they were visited.
Example 1:

Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0,0],[0,1],[0,2],[0,3]]
来自leetcode网站
Example 2:
Input: R = 5, C = 6, r0 = 1, c0 = 4
Output: [[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]
来自leetcode网站

Note:
1 <= R <= 100
1 <= C <= 100
0 <= r0 < R
0 <= c0 < C

这个题目稍微复杂一点,看例子2会更好理解。输入 R C r0 c0。R C 分别为数组的行数和列数,该数组为目标数组。 r0 c0 为起始点,按照螺旋形顺时针从里到外旋转。按照顺序记录旋转的路径和目标数组交集在数组中的位置,然后按照顺序输出所有位置。
基本思路:
按照要求运行螺旋形旋转逻辑,判断是否在目标数组范围内,在范围内就记录位置。当记录的位置数量和数组的元素数量相等时就可以返回结果。

public class Solution {
    public int[][] SpiralMatrixIII(int R, int C, int r0, int c0) {
            if (R == 0) return null;
            int[][] result = new int[R*C][];
            for (int i = 0; i < R * C; i++)
            {
                result[i] = new int[2];
            }
            int count = 1;
            int cur_x = r0, cur_y = c0;
            result[0][0] = r0;
            result[0][1] = c0;
            int layer = 1;
            int toward = 1;
            while (count < R * C)
            {
                bool skip = false;
                switch (toward)
                {
                    case 1:
                        if (cur_y + 1 <= (c0+layer))
                        {
                            ++cur_y;
                        }
                        else
                        {
                            toward++;
                            skip = true;
                        }
                        break;
                    case 2:
                        if (cur_x + 1 <= (r0 +layer))
                        {
                            ++cur_x;
                        }
                        else
                        {
                            toward++;
                            skip = true;
                        }
                        break;
                    case 3:
                        if (cur_y - 1 >= (c0-layer))
                        {
                            --cur_y;
                        }
                        else
                        {
                            toward++;
                            skip = true;
                        }
                        break;
                    case 4:
                        if (cur_x - 1 >=r0-layer)
                        {
                            --cur_x;
                        }
                        else
                        {
                            toward = 1;
                            layer++;
                            skip = true;
                        }
                        break;
                }
                if (!skip&&0 <= cur_x && cur_x < R && 0 <= cur_y && cur_y < C)
                {
                    result[count][0] = cur_x;
                    result[count][1] = cur_y;
                    count++;
                }
            }
            return result;
    }
}

56. Merge Intervals
Given an array of intervals where intervals[i] = [starti, endi], merge all overlapping intervals, and return an array of the non-overlapping intervals that cover all the intervals in the input.

Example 1:
Input: intervals = [[1,3],[2,6],[8,10],[15,18]]
Output: [[1,6],[8,10],[15,18]]
Explanation: Since intervals [1,3] and [2,6] overlaps, merge them into [1,6].

Example 2:
Input: intervals = [[1,4],[4,5]]
Output: [[1,5]]
Explanation: Intervals [1,4] and [4,5] are considered overlapping.

一组整型数组 intervals[i] = [starti, endi],starti, endi 分别表示起始和结束,合并数组。具体意思看上面的例子。
我看了下网站的解题思路,还是选择先排序然后再合并。代码如下:

public class Solution {
    public int[][] Merge(int[][] intervals) {
        if (intervals.Length <= 1) return intervals;
            Array.Sort(intervals, (x, y) => x[0].CompareTo(y[0]));
            List<int[]> result = new List<int[]>();
            int cur_min = intervals[0][0];
            int cur_max = intervals[0][1];
            for (int i = 1; i < intervals.Length; i++)
            {
                if (intervals[i][0] <= cur_max)
                {
                    cur_max = Math.Max(cur_max,intervals[i][1]);
                }
                else 
                {
                    result.Add(new int[] { cur_min, cur_max });
                    cur_min = intervals[i][0];
                    cur_max = intervals[i][1];
                }
            }
            result.Add(new int[] { cur_min, cur_max });
            return result.ToArray();
    }
}

这个题目没那么复杂,不过 Array.Sort 的用法可以好好学习下:
https://docs.microsoft.com/zh-cn/dotnet/api/system.array.sort?redirectedfrom=MSDN&view=net-5.0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值