美文网首页
letcode 矩阵-day

letcode 矩阵-day

作者: hehehehe | 来源:发表于2023-03-01 14:27 被阅读0次

118. 杨辉三角

class Solution:
    def generate(self, numRows: int) -> List[List[int]]:
        ret = list()
        for i in range(numRows):
            row = list()
            for j in range(0, i + 1):
                if j == 0 or j == i:
                    row.append(1)
                else:
                    row.append(ret[i - 1][j] + ret[i - 1][j - 1])
            ret.append(row)
        return ret

64. 最小路径和

https://leetcode.cn/problems/minimum-path-sum/solution/zui-xiao-lu-jing-he-by-leetcode-solution/

class Solution:
    def minPathSum(self, grid: List[List[int]]) -> int:
        if not grid or not grid[0]:
            return 0
        
        rows, columns = len(grid), len(grid[0])
        dp = [[0] * columns for _ in range(rows)]
        dp[0][0] = grid[0][0]
        for i in range(1, rows):
            dp[i][0] = dp[i - 1][0] + grid[i][0]
        for j in range(1, columns):
            dp[0][j] = dp[0][j - 1] + grid[0][j]
        for i in range(1, rows):
            for j in range(1, columns):
                dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        
        return dp[rows - 1][columns - 1]

面试题 01.07. 旋转矩阵

给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。

class Solution {
    public void rotate(int[][] matrix) {
        int row = matrix.length;
        int col = matrix[0].length;
        int[][] nums = new int[row][col];
        for(int i = 0 ; i < row ; i++){
            for(int j = 0 ; j < col ; j++){
                nums[j][col - i - 1] = matrix[i][j];
            }
        }
        for(int i = 0 ; i < row ; i++){
            for(int j = 0 ; j < col ; j++){
                matrix[i][j] = nums[i][j];
            }
        }
    }
}

相关文章

  • Median of Two Sorted Arrays

    标签(空格分隔): C++ 算法 LetCode 数组 每日算法——letcode系列 问题 Median of ...

  • Add Two Numbers

    每日算法——letcode系列 标签: 算法 C++ LetCode 问题 Add Two Numbers Di...

  • Longest Common Prefix

    标签: C++ 算法 LetCode 字符串 每日算法——letcode系列 问题 Longest Common ...

  • Roman to Integer

    标签: C++ 算法 LetCode 字符串 每日算法——letcode系列 问题 Roman to Intege...

  • 3Sum

    标签(空格分隔): C++ 算法 LetCode 数组 每日算法——letcode系列 问题 Longest Co...

  • letcode 1-day

    两数之和[https://leetcode-cn.com/problems/two-sum] 整数反转[https...

  • letcode 2-day

    https://www.cnblogs.com/liuzhen1995/p/13767751.html[https...

  • ZigZag Conversion

    每日算法——letcode系列 问题 ZigZag Conversion Difficulty: Easy The...

  • 算法练习进阶过程

    LetCode做题 看时间复杂度,也就是耗时

  • Longest Substring Without Repeat

    每日算法——letcode系列 问题 Longest Substring Without Repeating Ch...

网友评论

      本文标题:letcode 矩阵-day

      本文链接:https://www.haomeiwen.com/subject/gfjlldtx.html