package com.heu.wsq.leetcode.matrix;
/**
* 48. 旋转图像
* @author wsq
* @date 2020/12/19
* 给定一个 n × n 的二维矩阵表示一个图像。
* 将图像顺时针旋转 90 度。
* 说明:
* 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。
*
* 示例 1:
* 给定 matrix =
* [
* [1,2,3],
* [4,5,6],
* [7,8,9]
* ],
*
* 原地旋转输入矩阵,使其变为:
* [
* [7,4,1],
* [8,5,2],
* [9,6,3]
* ]
*
* 链接:https://leetcode-cn.com/problems/rotate-image
*/
public class RotateMatrix {
/**
* 将旋转转为翻转问题
* 水平翻转 + 主对角线翻转
* 矩阵顺时针旋转90°后,元素的对应位置为:
* matrix[row][col] = matrix[col][n - row - 1]
* @param matrix
*/
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;
}
}
}
public static void main(String[] args) {
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
RotateMatrix rm = new RotateMatrix();
rm.rotate(matrix);
int n = matrix.length;
for (int i = 0; i < n; i++) {
System.out.print("[");
for (int item : matrix[i]) {
System.out.print(item + " ");
}
System.out.println("]");
}
}
}
相关推荐
【eNSP 华为模拟器】单臂路由实战:从原理到配置全解析
本文详细解析了单臂路由技术,通过华为eNSP模拟器从原理到配置进行实战教学。文章阐述了单臂路由如何利用路由器单物理接口创建多个子接口,实现不同VLAN间通信,并提供了完整的操作步骤、配置命令及排错方法,帮助网络工程师掌握这一经典网络技术。
nCode:DesignLife案例教程三
nCode : DesignLife 案例三案例文件连接图标定义分析流程 本案例使用点焊分析图示符计算点焊钢制造的疲劳寿命。它是汽车车身的一部分,在轮拱周围和上方支撑车轮的主要垂直载荷(减震塔)。 案例文件 位于nCode安装目录下\demo\designlife\03_SpotWeldFatigue\: shock_loads.s3t shock_spot.op2 将上述文件复制到工作文件夹中,并使用副本文件完成该实例。 连接图标 将所有必须使用的图标拖入工作区: 从Input选项板中,将FE I
微信聊天记录解密,必须是root的手机
手机上安装x-plore软件,将/data/data目录的相关EnMicroMsg.db auth_info_key_prefs.xml拷贝到/storge/emulated/0目录后才可以拷贝到电脑。路径:/data/data/com.tencent.mm/shared_prefs/auth_info_key_prefs.xml。但是是加密过的 需要解密 解密密码生成规则是手机IMEI+微信UIN md5(32位小写)后的前7位。安卓手机要root测试版本微信要7或者8版本都可以。选择message表。
力扣48.旋转图像
力扣48.旋转图像这道题要求把给定矩阵旋转90度,并且不允许使用额外矩阵来完成旋转图像。于是这道题只能使用原地旋转的方法来解决。
动态博弈、威胁与承诺(博弈论入门学习笔记四)
0 动态博弈 动态博弈:行动有先后顺序,不同的参与人在不同时点行动,先行动者的选择影响后行动者的选择空间,后行动者可以观察到先行动者做了什么选择。例如下棋、消费者和商家讨价还价、谈婚论嫁、企业之间的价格战等等。 因此,为了做出最优的行动选择,每个参与人都应该思考:如果我如此选择,对方将如何应对?如果我是他,我将会如何行动?给定他的应对,什么是我的最优选择? 动态博弈中的战略:在博弈开始之前就...
继电器管理策略(二)
介绍了电池管理系统中的继电器管理,包含控制和诊断
LeetCode48-旋转图像
LeetCode48-旋转图像 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2]...
产品缺陷检测:表面缺陷检测_(1).产品缺陷检测基础理论
确定像素之间的相似性准则,如灰度值的差异。
[LeeCode] 48.旋转图像
48.旋转图像 难度:Medium 题目描述: 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,
微信小程序 - 实现Java微信支付退款功能以及退款流程,微信小程序前端+java后端完整示例源码,微信小程序退款功能详解,小程序订单申请退款和退款回调方法、查询退款等(新版微信支付,详细示例代码)
微信小程序开发-微信支付退款功能以及退款回调方法,微信支付退款API详解,微信小程序支付V3(支付、退款)Java实现微信小程序开发 小程序支付开发-小程序退款功能,微信小程序小程序和公众号 退款功能教程(含申请退款和退款回调,退款回调地址在商户后台配置或者代码自定义)最新版微信小程序支付,退款问题,小程序支付和退款功能详解,小程序申请退款,小程序处理退款,微信小程序支付与退款java,微信小程序退款及回调,JAVA实现微信小程序支付退款功能,退款api调用报错,如何解决退款申请无响应,如何用实现小程序订单
PCL 加权局部最优投影(WLOP)简化算法
WLOP算法是一种点云重采样方法,通过迭代优化实现点云均匀化和特征保留。算法包含数据项(加权平均位置保持几何特征)和正则化项(排斥邻近点实现均匀分布)两部分。代码实现基于PCL库,通过KD树加速邻域搜索,使用高斯权重函数计算点位置更新。参数包括输出点比例、邻域半径、正则化权重和迭代次数。该算法不依赖网格信息,适合处理大规模扫描数据的预处理优化。
[LeeCode] 48. 旋转图像
这三种方法,只有第二种符合咱们本题的要求,因为本题明确要求用原地旋转实现。但是我们今天还是按顺序讲解,主要是讲完第一种实现方案会对大家了解第二种有帮助。一、使用辅助数组(这是最容易想到和最容易实现的,但是它并符合咱们的题目要求)。我们需要使用一个和 matrix 大小相同的辅助数组。时间复杂度:O(N2),其中 N 是 matrix 的边长。旋转图像,这意味着你需要直接修改输入的二维矩阵。请你将图像顺时针旋转 90 度。二、原地旋转 (这是最符合本题要求的实现方式)使用另一个矩阵来旋转图像。
嵌入式工程师成长之路(10)——项目举例
送给大学毕业后找不到奋斗方向的你(每周不定时更新)
LeetCode 48:旋转图像
目录LeetCode 48:旋转图像题目描述解题 LeetCode 48:旋转图像 题目描述 给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。 不占用额外内存空间能否做到? 【示例 1】 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 【示例 2】 给定 matrix = [ [ 5, 1, 9,11], [ 2
VC中sqlite的使用
在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示
数学建模模型总结
这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!
leetcode 48.旋转图像
leetcode 48.旋转图像 从线性代数的角度,旋转矩阵有其他解法,此处仅考虑单纯的swap操作实现的旋转图像 再看看题目,重点要求原地旋转,换句话说不可以使用额外矩阵来做中间存储再写回,因此直接循环赋值的做法放弃。 要求整体顺时针旋转90度,此时我们只考虑单个元素的转移策略。由此三阶矩阵可以分为两层考虑,即最外面一层和5单独的一层,同理考虑二阶矩阵为一层,四阶也为两层,五阶为三层,可以推导公式,level = (n + 1) / 2 所以我们需要每层做个循环,然后每层单独处理。 再考虑每个层内部
SIMATIC S7-1200, S7-1500 PID 控制[手册]
SIMATIC S7-1200, S7-1500 PID 控制[手册]
Leetcode 48.旋转图像
Leetcode 48.旋转图像 题目描述 给定一个 n×n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 来源:力扣(LeetCode) 解题思路 当没有好的方法的时候就用暴力和模拟 暴力是不可能了 只有模拟了 然后就是 爱的魔力转圈圈 一圈一圈的转到最后...
深度学习工具包 DeepLearningToolbox
深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[](https://bitdeli.com/free "Bitdeli Badge")
LeetCode 48 旋转图像
leetcode 48 旋转图像 题目链接 思路: 如果使用辅助矩阵的话,难度并不大,但是题目要求使用O(1)的空间,所以就不能使用辅助矩阵。 不管用还是不用辅助矩阵,都应该先推一下公式: (x,y)旋转之后到了(y, n - x - 1),对于矩阵中的元素(x, y),如果直接把它赋值给(y, n - x - 1)的话,接下来把(y, n - x - 1)的元素旋转的时候,就会发现它的值已经在之前被覆盖了,所以需要临时变量保存。 注意到题目要求将矩阵顺时针旋转90°,那么旋转4次即可转回来, 对于(i,
LeetCode 48. 旋转图像:原地旋转最优解法
本文解析了LeetCode第48题「旋转图像」的原地解法,重点介绍了按圈旋转、四元素循环交换的最优实现。该算法通过逐层处理矩阵,每次交换四个对应位置的元素,实现O(n²)时间复杂度和O(1)空间复杂度。文章详细拆解了代码逻辑,包括边界处理、圈数计算和核心交换步骤,并通过3×3矩阵实例演示了旋转过程。同时指出了常见误区如索引颠倒、赋值顺序错误等。最后还提供了另一种简洁解法思路(转置+反转)供参考。这种原地操作技巧对掌握数组类题目具有重要价值。
LeetCode刷题之48.旋转图像
LeetCode刷题之48.旋转图像 我不知道将去向何方,但我已在路上! 时光匆匆,虽未曾谋面,却相遇于斯,实在是莫大的缘分,感谢您的到访 ! 题目: 给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例: 示例 1: 给定 mat...
48. 旋转图像(Rotate Image)
48. 旋转图像(Rotate Image)题解直接旋转复杂度分析PythonJava(待完成)转置矩阵的性质复杂度分析PythonJava(待完成) 题解 直接旋转 图像旋转,实际上是这四个位置上的数对应旋转。 因此,需要找到这四个位置索引的相互关系: matrix[i][j]→matrix[j][n−i−1]→matrix[n−i−1][n−j−1]→matrix[n−j−1][i]matr...
LeetCode 48. 旋转图像
题目:48. 旋转图像 类别:数组 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1],...
力扣题解48. 旋转图像
力扣题解48. 旋转图像题解题思路代码结束语 题 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10
LeetCode 48 旋转图像 Java实现 两种方法
本题考察了对矩阵操作的理解和实现能力。通过两种不同的方法,我们展示了如何高效地解决矩阵旋转问题。无论是分层旋转法还是数学变换法,都有其独特的优点,开发者可以根据实际需求选择合适的方法。
Leetcode 48. 旋转图像
Leetcode 48. 旋转图像 题目说明 给定一个 n × n 的二维矩阵表示一个图像。 将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9] ], 原地旋转输入矩阵,使其变为: [ [7,4,1], [8,5,2], [9,6,3] ] 示例 2: 给定 matrix = [ [ 5, 1, 9,11], [ 2, 4, 8,10],
698



被折叠的 条评论
为什么被折叠?



