首页 > 编程语言 >LeetCode算法笔记 566. 重塑矩阵

LeetCode算法笔记 566. 重塑矩阵

时间:2022-10-12 00:12:14浏览次数:70  
标签:输出 mat 示例 矩阵 566 重塑 LeetCode

import junit.framework.TestCase;

import java.util.Arrays;


public class LeetCode04_1 extends TestCase {


    /**
     * 566. 重塑矩阵
     * 在 MATLAB 中,有一个非常有用的函数 reshape ,它可以将一个 m x n 矩阵重塑为另一个大小不同(r x c)的新矩阵,但保留其原始数据。
     * 给你一个由二维数组 mat 表示的 m x n 矩阵,以及两个正整数 r 和 c ,分别表示想要的重构的矩阵的行数和列数。
     * 重构后的矩阵需要将原始矩阵的所有元素以相同的 行遍历顺序 填充。
     * 如果具有给定参数的 reshape 操作是可行且合理的,则输出新的重塑矩阵;否则,输出原始矩阵。
     * <p>
     * 示例 1:
     * 输入:mat = [[1,2],[3,4]], r = 1, c = 4
     * 输出:[[1,2,3,4]]
     * <p>
     * 示例 2:
     * 输入:mat = [[1,2],[3,4]], r = 2, c = 4
     * 输出:[[1,2],[3,4]]
     * <p>
     * 提示:
     * m == mat.length
     * n == mat[i].length
     * 1 <= m, n <= 100
     * -1000 <= mat[i][j] <= 1000
     * 1 <= r, c <= 300
     */

    /**
     * 将二维数组 nums 映射成一个一维数组;再将这个一维数组映射回 r 行 c 列的二维数组。
     * 主要注意 res[][] 的下标问题。
     *
     * 时间复杂度:O(rc)。这里的时间复杂度是在重塑矩阵成功的前提下的时间复杂度,否则当 mn !=rc 时,
     * C++ 语言中返回的是原数组的一份拷贝,本质上需要的时间复杂度为 O(mn),而其余语言可以直接返回原数组的对象,需要的时间复杂度仅为 O(1)。
     *
     * 空间复杂度:O(1)。这里的空间复杂度不包含返回的重塑矩阵需要的空间。
     * 
     */
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int m = mat.length;
        int n = mat[0].length;
        if (m * n != r * c) {
            return mat;
        }
        int[][] res = new int[r][c];
        for (int i = 0; i < m * n; i++) {
            res[i / c][i % c] = mat[i / n][i % n];
        }
        return res;
    }

    public void test() {
        int[][] mat = new int[][]{{1, 2}, {3, 4}};
        System.out.println(Arrays.deepToString(matrixReshape(mat, 1, 4)));
        System.out.println(Arrays.deepToString(matrixReshape(mat, 2, 4)));
    }
}

  

标签:输出,mat,示例,矩阵,566,重塑,LeetCode
From: https://www.cnblogs.com/sueyyyy/p/16783079.html

相关文章

  • LeetCode算法笔记 121. 买卖股票的最佳时机
    importjunit.framework.TestCase;publicclassLeetCode03_2extendsTestCase{/***121.买卖股票的最佳时机*给定一个数组prices,它的第i......
  • leetcode-67-easy
    AddBinary思路一:先计算公共部分,最后补充未计算的位置,模拟二进制加法,写的太丑了publicStringaddBinary(Stringa,Stringb){charONE='0'+'1';char......
  • LeetCode148. Sort List
    题意链表排序方法递归代码classSolution{public:ListNode*sortList(ListNode*head){returnsortList(head,nullptr);}ListNode*......
  • leetcode-26-easy
    RemoveDuplicatesfromSortedArray思路一:双指针,左指针永远指向有效数组长度+1的位置,左指针只会在出现交换后向右移动。右指针一直向右扫描,遇到不重复的数字就和左指......
  • leetcode-58-easy
    LengthofLastWord思路一:从后面非空格字符开始扫描,记录非空格字符个数。优化:不用char[],直接用charAt()判断publicintlengthOfLastWord(Strings){......
  • leetcode-66-easy
    PlusOne思路一:暴力,方向想错了,不能把digits当做一个整数看publicint[]plusOne(int[]digits){if(digits[digits.length-1]!=9){digits[digit......
  • #yyds干货盘点# LeetCode 热题 HOT 100:最小覆盖子串
    题目:给你一个字符串s、一个字符串t。返回s中涵盖t所有字符的最小子串。如果s中不存在涵盖t所有字符的子串,则返回空字符串""。 注意:对于t中重复字符,我们寻......
  • LeetCode88.合并两个数组
    1.题目描述给你两个按非递减顺序排列的整数数组nums1和nums2,另有两个整数m和n,分别表示nums1和nums2中的元素数目。请你合并nums2到nums1中,使合并后的......
  • LeetCode 1195. Fizz Buzz Multithreaded
    原题链接在这里:https://leetcode.com/problems/fizz-buzz-multithreaded/题目:Youhavethefourfunctions:printFizz thatprintstheword "fizz" totheconsole......
  • leetcode-394.字符串解码
    394.字符串解码publicStringdecodeString(Strings){Stack<Character>stack=newStack<>();for(charc:s.toCharArray()){if(c......