566. 重塑矩阵
解法一、遍历求解
题目描述实在是模糊得男默女泪。。非常基本的遍历,对着各归其位塞进去就好了
class Solution {
public int[][] matrixReshape(int[][] mat, int r, int c) {
int row = mat.length;
int col = mat[0].length;
if(r * c < row * col){
return mat;
}
int x = 0,y = 0;
int[][] res = new int[r][c];
for(int i = 0;i < row;i++){
for(int j = 0;j < col;j++){
res[x][y] = mat[i][j];
if(y == c - 1){
x++;
y = 0;
}else{
y++;
}
}
}
return res;
}
}
解法二、一维数组转换
相当于将原数组先拍扁再映射,非常聪明的坐标转换
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length;
int n = nums[0].length;
if (m * n != r * c) {
return nums;
}
int[][] ans = new int[r][c];
for (int x = 0; x < m * n; ++x) {
ans[x / c][x % c] = nums[x / n][x % n];
}
return ans;
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/reshape-the-matrix/solutions/606281/zhong-su-ju-zhen-by-leetcode-solution-gt0g/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
48. 旋转图像
解法一、遍历更换每圈
基本见注释,就是一圈一圈地改换,一次换四个位置,每圈初始位置从左上开始,一直到右上除最后一个。
好处:很快,符合直觉
坏处:费人(。
class Solution {
public static void rotate(int[][] matrix) {
int n = matrix.length;
int temp = 0;
if(n != 1) {
int count = 0;//象征着有count层
while (count < n){
for(int i = count;i < n - 1 - count;i++){
temp = matrix[i][n - 1 - count];//保存②的值
matrix[i][n - 1 - count] = matrix[count][i];//把②换成①
matrix[count][i] = matrix[n-1-i][count];//把①的值换成④的值
matrix[n-1-i][count] = matrix[n-count-1][n-1-i];//把④的值换成③的值
matrix[n-count - 1][n-1-i] = temp;//把③的值换成②的值
}
count++;
}
}
}
}
解法二、 水平再对角线变幻
代码好想,不过作为数学问题(90°翻转=转置再水平)需要想一下
matrix[row][col],在旋转后,它的新位置为 matrixnew[col][n−row−1]←关键等式
class Solution {
public void rotate(int[][] matrix) {
int n = matrix.length;
// 水平翻转
for (int i = 0; i < n / 2; ++i) {
for (int j = 0; j < n; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[n - i - 1][j];
matrix[n - i - 1][j] = temp;
}
}
// 主对角线翻转
for (int i = 0; i < n; ++i) {
for (int j = 0; j < i; ++j) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/rotate-image/solutions/526980/xuan-zhuan-tu-xiang-by-leetcode-solution-vu3m/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
73. 矩阵置零
解法一、标记所有0的位置,然后再遍历设置
不用猜就知道不是最佳算法,时间复杂度肯定超了
其实也不符合空间复杂度O(1)的要求
class Solution {
public static void setZeroes(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
int[][] a = new int[row*col][2];
int num = 0;
for(int i = 0;i< row;i++){
for(int j = 0;j < col;j++){
if(matrix[i][j] == 0){
a[num][0] = i;
a[num][1] = j;
num++;
}
}
}
for(int i = 0;i < num;i++){
for(int j = 0;j < row;j++){
matrix[j][a[i][1]] = 0;
}
for(int j = 0;j < col;j++){
matrix[a[i][0]][j] = 0;
}
}
}
}
解法二、第一行/列代替额外数组
还是扫一遍确认、扫一遍执行的套路,但是用第一行和第一列的变量来储存数组位置。
class Solution {
public void setZeroes(int[][] matrix) {
int m = matrix.length, n = matrix[0].length;
boolean flagCol0 = false, flagRow0 = false;
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
flagCol0 = true;
}
}
for (int j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
flagRow0 = true;
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = matrix[0][j] = 0;
}
}
}
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (flagCol0) {
for (int i = 0; i < m; i++) {
matrix[i][0] = 0;
}
}
if (flagRow0) {
for (int j = 0; j < n; j++) {
matrix[0][j] = 0;
}
}
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/set-matrix-zeroes/solutions/669901/ju-zhen-zhi-ling-by-leetcode-solution-9ll7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
289. 生命游戏
解法一、遍历寻找,按部就班
按照题解写然后分类讨论就是了(完全没有优化,感觉这么高的双击败有点德不配位)
class Solution {
public static void gameOfLife(int[][] board) {
int r = board.length;
int c = board[0].length;
int[][] res = new int[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
int temp = sum(board, i,j);
if (board[i][j] == 1) {
if (temp < 2 || temp > 3) {
res[i][j] = 0;
}else{
res[i][j] = 1;
}
} else {
if (temp == 3) {
res[i][j] = 1;
}
}
}
}
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
board[i][j] = res[i][j];
}
}
}
public static int sum(int[][]board,int x,int y){
int sum = 0;
int minR = Math.max(0,x-1);
int maxR = Math.min(board.length-1,x+1);
int minC = Math.max(0,y-1);
int maxC = Math.min(board[0].length-1,y+1);
for(int i = minR;i <= maxR;i++){
for(int j = minC;j <= maxC;j++){
if(board[i][j] == 1 && (i != x || j != y)){
sum++;
}
}
}
return sum;
}
}
解法二、原地哈希(类似)
就是在不同的情况下设置状态。原来需要复制一个是为了防止修改后影响其他细胞位置统计,现在只要让修改后的位置也满足统计的方式就好。结尾再遍历一次,恢复0和1。
也有人会用位运算,但是感觉这样已经足够了,也具有可视度
另:表驱动查找1-9位置也很有趣
class Solution {
public void gameOfLife(int[][] board) {
int[] neighbors = {0, 1, -1};
int rows = board.length;
int cols = board[0].length;
// 遍历面板每一个格子里的细胞
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
// 对于每一个细胞统计其八个相邻位置里的活细胞数量
int liveNeighbors = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (!(neighbors[i] == 0 && neighbors[j] == 0)) {
// 相邻位置的坐标
int r = (row + neighbors[i]);
int c = (col + neighbors[j]);
// 查看相邻的细胞是否是活细胞
if ((r < rows && r >= 0) && (c < cols && c >= 0) && (Math.abs(board[r][c]) == 1)) {
liveNeighbors += 1;
}
}
}
}
// 规则 1 或规则 3
if ((board[row][col] == 1) && (liveNeighbors < 2 || liveNeighbors > 3)) {
// -1 代表这个细胞过去是活的现在死了
board[row][col] = -1;
}
// 规则 4
if (board[row][col] == 0 && liveNeighbors == 3) {
// 2 代表这个细胞过去是死的现在活了
board[row][col] = 2;
}
}
}
// 遍历 board 得到一次更新后的状态
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
if (board[row][col] > 0) {
board[row][col] = 1;
} else {
board[row][col] = 0;
}
}
}
}
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/game-of-life/solutions/179750/sheng-ming-you-xi-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
碎碎念
- 增加了对坐标的控制,最近的题感觉都数学比较着重,没学什么数据结构