Share 01 matrix leetcode java,

ngocdanlediep

New member
#Java, #LeetCode, #Matrix, #AlGorithM
## 01 Ma trận LeetCode Java

Trong bài viết này, chúng tôi sẽ giải quyết vấn đề [Matrix Transpose] (https://leetcode.com/problems/matrix-transpose/) trong Java.Vấn đề là chuyển một ma trận, có nghĩa là trao đổi các hàng và cột của ma trận.

### Vấn đề

Cho một ma trận A có kích thước `m x n`, trả lại chuyển đổi của A, là ma trận B có kích thước` n x m` trong đó `b [j] == a [j] `và `j`.

### Giải pháp

Chúng ta có thể giải quyết vấn đề này trong thời gian O (Mn) bằng thuật toán đơn giản.Thuật toán hoạt động bằng cách lặp trên các hàng của ma trận A và hoán đổi các phần tử trong mỗi hàng với các phần tử tương ứng trong ma trận chuyển đổi B.

`` `java
public tĩnh int [] [] chuyển đổi (int [] [] a) {
int m = a.length;
int n = a [0] .length;
int [] [] b = new int [n] [m];

for (int i = 0; i <m; i ++) {
for (int j = 0; j <n; j ++) {
B [j] = a [j];
}
}

trả lại b;
}
`` `

### Độ phức tạp thời gian

Độ phức tạp thời gian của thuật toán này là o (mn), trong đó `m` là số lượng hàng trong ma trận A và` n` là số lượng cột trong ma trận A. Điều này là do chúng ta cần lặp lại trên tất cảCác phần tử trong ma trận A để trao đổi chúng với các phần tử tương ứng trong ma trận chuyển vị B.

### Độ phức tạp không gian

Độ phức tạp không gian của thuật toán này là o (mn), trong đó `m` là số lượng hàng trong ma trận A và` n` là số lượng cột trong ma trận A. Điều này là do chúng ta cần tạo một ma trận B mới B mới B mớicó kích thước `n x m` để lưu trữ ma trận chuyển vị.

### Ví dụ

`` `
Đầu vào:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

Đầu ra:
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
`` `

### Người giới thiệu

* [LeetCode - Chuyển đổi ma trận] (https://leetcode.com/problems/matrix-transpose/)
=======================================
#Java, #LeetCode, #Matrix, #AlGorithM
## 01 Matrix Leetcode Java

In this article, we will solve the [Matrix Transpose](https://leetcode.com/problems/matrix-transpose/) problem in Java. The problem is to transpose a matrix, which means to swap the rows and columns of the matrix.

### Problem

Given a matrix A of size `m x n`, return the transpose of A, which is a matrix B of size `n x m` where `B[j] == A[j]` for all `i` and `j`.

### Solution

We can solve this problem in O(mn) time using a simple algorithm. The algorithm works by iterating over the rows of the matrix A and swapping the elements in each row with the corresponding elements in the transposed matrix B.

```java
public static int[][] transpose(int[][] A) {
int m = A.length;
int n = A[0].length;
int[][] B = new int[n][m];

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
B[j] = A[j];
}
}

return B;
}
```

### Time Complexity

The time complexity of this algorithm is O(mn), where `m` is the number of rows in the matrix A and `n` is the number of columns in the matrix A. This is because we need to iterate over all of the elements in the matrix A in order to swap them with the corresponding elements in the transposed matrix B.

### Space Complexity

The space complexity of this algorithm is O(mn), where `m` is the number of rows in the matrix A and `n` is the number of columns in the matrix A. This is because we need to create a new matrix B of size `n x m` to store the transposed matrix.

### Example

```
Input:
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

Output:
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
```

### References

* [LeetCode - Matrix Transpose](https://leetcode.com/problems/matrix-transpose/)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top