logseq_notes/journals/2023_04_21.md

86 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

2023-06-14 14:27:22 +08:00
- 中国地下乐队
collapsed:: true
- 退格 backspace (psychedelic rock
- 兵马司(发行)
- Re tros (daft funk
- Dream can
- Leetcode - Rotate Image
id:: 64422d1f-5db3-435d-ab4e-2b9fe5fd47cd
collapsed:: true
- Times:
- Time when completed: 14:28
- Time taken to complete: 15 mins ( with solution in mind )
- DONE Revisions:
SCHEDULED: <2023-05-26 Fri>
:LOGBOOK:
* State "DONE" from "LATER" [2023-04-28 Fri 16:45]
* State "DONE" from "LATER" [2023-05-23 Tue 20:26]
:END:
-
- Tags:
- Algorithms: #math
- Data structures: #array
- Difficulty: #difficulty_easy
- Platforms: #leetcode
- Links:
- [link to the problem](https://leetcode.com/problems/rotate-image/description/)
- Problem:
- You are given an `n x n` 2D `matrix` representing an image, rotate the image by **90** degrees (clockwise).
- You have to rotate the image [**in-place**](https://en.wikipedia.org/wiki/In-place_algorithm), which means you have to modify the input 2D matrix directly. **DO NOT** allocate another 2D matrix and do the rotation.
- Examples:
- https://assets.leetcode.com/uploads/2020/08/28/mat1.jpg
- https://assets.leetcode.com/uploads/2020/08/28/mat2.jpg
- ```
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]]
Output: [[7,4,1],[8,5,2],[9,6,3]]
Example 2:
Input: matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
Output: [[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
```
- Constraints:
- `n == matrix.length == matrix[i].length`
- `1 <= n <= 20`
- `-1000 <= matrix[i][j] <= 1000`
- Thoughts:
- Intuition:
- The solution is tricky, which requires you to use math.
- In order to do this, first, transpose, then reflect the matrix.
- Solution:
- java
- ```java
class Solution {
private void swap(int[][] matrix, int a, int b, int x, int y) {
int temp = matrix[a][b];
matrix[a][b] = matrix[x][y];
matrix[x][y] = temp;
}
public void rotate(int[][] matrix) {
// transpose and reflect
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < i; j++) {
swap(matrix, i, j, j, i);
}
}
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length / 2; j++) {
swap(matrix, i, j, i, matrix.length - j - 1);
}
}
}
}
```
- DONE 购物清单 [[Shopping lists]]
- DONE 香熏/香氛
- DONE 耳塞
- DONE 查英语成绩
SCHEDULED: <2023-04-25 Tue>
- DONE 报名
SCHEDULED: <2023-05-08 Mon>
- DONE 写乐理练习的作业