LeetCode腾讯50题-Day7-62/70/78

LeetCode50题(17天)-Day7

54 螺旋矩阵

  • 题号:54
  • 难度:中等
  • https://leetcode-cn.com/problems/spiral-matrix/

给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素。

示例 1:

输入:
[
 [ 1, 2, 3 ],
 [ 4, 5, 6 ],
 [ 7, 8, 9 ]
]
输出: [1,2,3,6,9,8,7,4,5]

示例 2:

输入:
[
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9,10,11,12]
]
输出: [1,2,3,4,8,12,11,10,9,5,6,7]

示例 3:

输入:
[
  [1]
]
输出: [1]

示例 4:

输入:
[
  [2, 3, 4],
  [5, 6, 7],
  [8, 9, 10],
  [11, 12, 13]
]
输出: [2,3,4,7,10,13,12,11,8,5,6,9]

实现

  • 状态:通过
  • 22 / 22 个通过测试用例
  • 执行用时: 348 ms, 在所有 C# 提交中击败了 85.53% 的用户
  • 内存消耗: 28.9 MB, 在所有 C# 提交中击败了 5.26% 的用户
public class Solution
{
    public IList<int> SpiralOrder(int[][] matrix)
    {
        IList<int> result = new List<int>();
        if (matrix == null || matrix.Length == 0)
            return result;

        int start = 0;
        int end1 = matrix[start].Length - 1 - start;
        int end2 = matrix.Length - 1 - start;

        // 只有横着的情况
        if (start == end2)
        {
            LeftToRight(start, end1, start, matrix, result);
            return result;
        }
        //只有竖着的情况
        if (start == end1)
        {
            TopToBottom(start, end2, start, matrix, result);
            return result;
        }

        while (start < end1 && start < end2)
        {
            LeftToRight(start, end1, start, matrix, result);
            TopToBottom(start + 1, end2, end1, matrix, result);
            RightToLeft(end1 - 1, start, end2, matrix, result);
            BottomToTop(end2 - 1, start + 1, start, matrix, result);
            start++;
            end1 = matrix[start].Length - 1 - start;
            end2 = matrix.Length - 1 - start;
        }
        // 只剩下横着的情况
        if (start == end2)
        {
            LeftToRight(start, end1, start, matrix, result);
        }
        else if (start == end1)
        {
            //只剩下竖着的情况
            TopToBottom(start, end2, start, matrix, result);
        }
        return result;
    }

    private void LeftToRight(int start, int end, int rowIndex, int[][] matrix, IList<int> lst)
    {
        for (int i = start; i <= end; i++)
        {
            lst.Add(matrix[rowIndex][i]);
        }
    }

    private void TopToBottom(int start, int end, int colIndex, int[][] matrix, IList<int> lst)
    {
        for (int i = start; i <= end; i++)
        {
            lst.Add(matrix[i][colIndex]);
        }
    }

    private void RightToLeft(int start, int end, int rowIndex, int[][] matrix, IList<int> lst)
    {
        for (int i = start; i >= end; i--)
        {
            lst.Add(matrix[rowIndex][i]);
        }
    }

    private void BottomToTop(int start, int end, int colIndex, int[][] matrix, IList<int> lst)
    {
        for (int i = start; i >= end; i--)
        {
            lst.Add(matrix[i][colIndex]);
        }
    }
}

59 螺旋矩阵2

  • 题号:59
  • 难度:中等
  • https://leetcode-cn.com/problems/spiral-matrix-ii/

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

示例:

输入: 3
输出:
[
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
]

实现

  • 状态:通过
  • 20 / 20 个通过测试用例
  • 执行用时: 296 ms, 在所有 C# 提交中击败了 97.67% 的用户
  • 内存消耗: 25 MB, 在所有 C# 提交中击败了 11.11% 的用户
public class Solution
{
    public int[][] GenerateMatrix(int n)
    {
        int[][] matrix = new int[n][];
        for (int i = 0; i < n; i++)
        {
            matrix[i] = new int[n];
        }

        int start = 0;//起始位置
        int end1 = n - 1;//最左边位置
        int end2 = n - 1;//最下边位置
        int count = 1;

        while (start < end1 && start < end2)
        {
            LeftToRight(start, end1, start, matrix, ref count);
            TopToBottom(start + 1, end2, end1, matrix, ref count);
            RightToLeft(end1 - 1, start, end2, matrix, ref count);
            BottomToTop(end2 - 1, start + 1, start, matrix, ref count);
            start++;
            end1 = n - 1 - start;
            end2 = n - 1 - start;
        }
        if (n%2 == 1)
        {
            matrix[start][start] = count;
        }
        return matrix;
    }

    private void LeftToRight(int start, int end, int rowIndex, int[][] matrix, ref int from)
    {
        for (int i = start; i <= end; i++)
        {
            matrix[rowIndex][i] = from;
            from++;
        }
    }

    private void TopToBottom(int start, int end, int colIndex, int[][] matrix, ref int from)
    {
        for (int i = start; i <= end; i++)
        {
            matrix[i][colIndex] = from;
            from++;
        }
    }

    private void RightToLeft(int start, int end, int rowIndex, int[][] matrix, ref int from)
    {
        for (int i = start; i >= end; i--)
        {
            matrix[rowIndex][i] = from;
            from++;
        }
    }

    private void BottomToTop(int start, int end, int colIndex, int[][] matrix, ref int from)
    {
        for (int i = start; i >= end; i--)
        {
            matrix[i][colIndex] = from;
            from++;
        }
    }
}

61 旋转链表

  • 题号:61
  • 难度:中等
  • https://leetcode-cn.com/problems/rotate-list/

给定一个链表,旋转链表,将链表每个节点向右移动 k 个位置,其中 k 是非负数。

示例 1:

输入: 1->2->3->4->5->NULL, k = 2
输出: 4->5->1->2->3->NULL

解释:
向右旋转 1: 5->1->2->3->4->NULL
向右旋转 2: 4->5->1->2->3->NULL

示例 2:

输入: 0->1->2->NULL, k = 4
输出: 2->0->1->NULL

解释:
向右旋转 1: 2->0->1->NULL
向右旋转 2: 1->2->0->NULL
向右旋转 3: 0->1->2->NULL
向右旋转 4: 2->0->1->NULL

实现

  • 执行结果:通过
  • 执行用时:100 ms, 在所有 C# 提交中击败了 98.13% 的用户
  • 内存消耗:25.1 MB, 在所有 C# 提交中击败了 100.00% 的用户
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     public int val;
 *     public ListNode next;
 *     public ListNode(int x) { val = x; }
 * }
 */
 
public class Solution
{
    public ListNode RotateRight(ListNode head, int k)
    {
        if (head == null || k == 0)
            return head;

        int len = GetLength(head);
        int index = len - k%len;

        if (index == len)
            return head;

        ListNode temp1 = head;
        ListNode temp2 = head;
        for (int i = 0; i < index - 1; i++)
        {
            temp1 = temp1.next;
        }
        head = temp1.next;
        temp1.next = null;

        temp1 = head;
        while (temp1.next != null)
        {
            temp1 = temp1.next;
        }
        temp1.next = temp2;
        return head;
    }

    public int GetLength(ListNode head)
    {
        ListNode temp = head;
        int i = 0;
        while (temp != null)
        {
            i++;
            temp = temp.next;
        }
        return i;
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,我来用中文回复这个链接:https://leetcode-cn.com/tag/dynamic-programming/ 这个链接是 LeetCode 上关于动态规划的目集合。动态规划是一种常用的算法思想,可以用来解决很多实际问,比如最长公共子序列、背包问、最短路径等等。在 LeetCode 上,动态规划也是一个非常重要的型,很多目都需要用到动态规划的思想来解决。 这个链接里包含了很多关于动态规划的目,按照难度从简单到困难排列。每个目都有详细的目描述、输入输出样例、目解析和代码实现等内容,非常适合想要学习动态规划算法的人来练习和提高自己的能力。 总之,这个链接是一个非常好的学习动态规划算法的资源,建议大家多多利用。 ### 回答2: 动态规划是一种算法思想,通常用于优化具有重叠子问和最优子结构性质的问。由于其成熟的数学理论和强大的实用效果,动态规划在计算机科学、数学、经济学、管理学等领域均有重要应用。 在计算机科学领域,动态规划常用于解决最优化问,如背包问、图像处理、语音识别、自然语言处理等。同时,在计算机网络和分布式系统中,动态规划也广泛应用于各种优化算法中,如链路优化、路由算法、网络流量控制等。 对于算法领域的程序员而言,动态规划是一种必要的技能和知识点。在LeetCode这样的程序员平台上,目分类和标签设置十分细致和方便,方便程序员查找并深入学习不同类型的算法。 LeetCode的动态规划标签下的目涵盖了各种难度级别和场景的问。从简单的斐波那契数列、迷宫问到可以用于实际应用的背包问、最长公共子序列等,难度不断递进且话丰富,有助于开发人员掌握动态规划的实际应用技能和抽象思维模式。 因此,深入LeetCode动态规划分类下的目学习和练习,对于程序员的职业发展和技能提升有着重要的意义。 ### 回答3: 动态规划是一种常见的算法思想,它通过将问拆分成子问的方式进行求解。在LeetCode中,动态规划标签涵盖了众多经典和优美的算法问,例如斐波那契数列、矩阵链乘法、背包问等。 动态规划的核心思想是“记忆化搜索”,即将中间状态保存下来,避免重复计算。通常情况下,我们会使用一张二维表来记录状态转移过程中的中间值,例如动态规划求解斐波那契数列问时,就可以定义一个二维数组f[i][j],代表第i项斐波那契数列中,第j个元素的值。 在LeetCode中,动态规划标签下有众多难度不同的问。例如,经典的“爬楼梯”问,要求我们计算到n级楼梯的方案数。这个问的解法非常简单,只需要维护一个长度为n的数组,记录到达每一级楼梯的方案数即可。类似的问还有“零钱兑换”、“乘积最大子数组”、“通配符匹配”等,它们都采用了类似的动态规划思想,通过拆分问、保存中间状态来求解问。 需要注意的是,动态规划算法并不是万能的,它虽然可以处理众多经典问,但在某些场景下并不适用。例如,某些问的状态转移过程比较复杂,或者状态转移方程中存在多个参数,这些情况下使用动态规划算法可能会变得比较麻烦。此外,动态规划算法也存在一些常见误区,例如错用贪心思想、未考虑边界情况等。 总之,掌握动态规划算法对于LeetCode的学习和解都非常重要。除了刷以外,我们还可以通过阅读经典的动态规划书籍,例如《算法竞赛进阶指南》、《算法与数据结构基础》等,来深入理解这种算法思想。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值