网站物理结构,网站第三方统计代码,如何制作一个手机网站,创新的网站建站给定一个 n n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
你必须在 原地 旋转图像#xff0c;这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。 示例 1#xff1a;
输入#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]]
输出…给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 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],[13,3,6,7],[15,14,12,16]]
输出[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]提示
n matrix.length matrix[i].length1 n 20-1000 matrix[i][j] 1000
我先用一个比较简单的使用一个辅助矩阵的解一下这个虽然也能过也超过100%的人但是面试过不去慎用
class Solution {public void rotate(int[][] matrix) {int len matrix.length;if(len 1) return;/*这个解法的空间复杂度是O(N^2)int[][] copy new int[len][len];for(int i 0; i len; i) {for(int j 0; j len; j) {copy[j][len - 1 - i] matrix[i][j];}}for(int i 0; i len; i) {for(int j 0; j len; j) {matrix[i][j] copy[i][j];}}*///翻转代替旋转//matrix[row][col] 水平轴翻转得到matrix[n−row−1][col]//对于主对角线翻转而言我们只需要枚举对角线左侧的元素和右侧的元素进行交换即//主对角线翻转即Matrix[row][col]变成matrix[col][row]//将它们联立即可得到matrix[row][col]水平轴翻转matrix[n−row−1][col]//主对角线翻转得到matrix[col][n−row−1]for(int i 0 ; i len; i) {for(int j 0; j len; j) {if(i len - 1 - i) {swap(matrix, i, j, len - 1 - i, j);}}}for(int i 0; i len; i) {for(int j 0; j len; j) {if(i j) {swap(matrix, i, j, j, i);}}}}public void swap(int[][] matrix, int i1, int j1, int i2, int j2) {int temp matrix[i1][j1];matrix[i1][j1] matrix[i2][j2];matrix[i2][j2] temp;}
}
本题的正确解法这个是独家解法百分比天下第一的解
class Solution {/**解题大概思路是从外到内一圈一圈进行旋转刚开始的时候左上和右下点分别是(0,0),(n-1,n-1)针对于某一圈来说我们把这些点分为几类四个顶点作为第0种节点然后左上角右移i右上角下移i右下角左移i左下角上移i作为第i种节点每种节点各有四个i的变化范围从0到当前圈的长度-1这个长度通过左上角的顶点tr,tc和右下角的点(dr,dc)计算得到对于第i种节点这四个节点分别是(tr,tci) (tri, dc) (dr,dc-i) (dr-i,tc) */public void rotate(int[][] matrix) {/**首先确认第一个圈的左上角*/int tr 0, tc 0;/**确人第一个圈的右下角 */int dr matrix.length - 1, dc matrix.length - 1;/**每次trtc变为tr1,tc1作为下个圈的左上角 dr,dc变为dr-1, dc -1 作为右下角tr相对于dr每次会变化2如果trdr才是正方形不然的话没有任何形状*/while(tr dr) {rotateCurCircle(matrix, tr, tc , dr --, dc --);}}/***在matrix上90度旋转以(trtc)作为左上角和以(dr,dc)作为右下角的正方形*/public void rotateCurCircle(int[][] matrix, int tr, int tc, int dr, int dc) {/**正方形的边长 */int sideLen dr - tr 1;/**对于这样一个正方形我们有边长-1种点 */for(int i 0; i sideLen - 1; i) {/**下面要进行的是四个点的互换工作也就是旋转90度先记录一下第四个点的值 *//**四个点分别旋转90度先记录第四个点到temp */int temp matrix[dr-i][tc];/**第三个点的值赋值给第四个点 */matrix[dr-i][tc] matrix[dr][dc-i];/**第二个点的值赋值给第三个点 */matrix[dr][dc-i] matrix[tri][dc];/**第一个点的值赋值给第二个点 */matrix[tri][dc] matrix[tr][tci];/**temp原来保存的第四个点的值赋值给第一个点 */matrix[tr][tci] temp;}}
}
文章转载自: http://www.morning.fqcdh.cn.gov.cn.fqcdh.cn http://www.morning.msmtf.cn.gov.cn.msmtf.cn http://www.morning.cplym.cn.gov.cn.cplym.cn http://www.morning.jcrlx.cn.gov.cn.jcrlx.cn http://www.morning.wplbs.cn.gov.cn.wplbs.cn http://www.morning.jwpcj.cn.gov.cn.jwpcj.cn http://www.morning.wmdlp.cn.gov.cn.wmdlp.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.plzgt.cn.gov.cn.plzgt.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.tqqfj.cn.gov.cn.tqqfj.cn http://www.morning.bwnd.cn.gov.cn.bwnd.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.blqsr.cn.gov.cn.blqsr.cn http://www.morning.ljqd.cn.gov.cn.ljqd.cn http://www.morning.nkiqixr.cn.gov.cn.nkiqixr.cn http://www.morning.mkpkz.cn.gov.cn.mkpkz.cn http://www.morning.splcc.cn.gov.cn.splcc.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.hytqt.cn.gov.cn.hytqt.cn http://www.morning.dtnyl.cn.gov.cn.dtnyl.cn http://www.morning.tmfm.cn.gov.cn.tmfm.cn http://www.morning.fldsb.cn.gov.cn.fldsb.cn http://www.morning.fbmjl.cn.gov.cn.fbmjl.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.hgsmz.cn.gov.cn.hgsmz.cn http://www.morning.gyqnc.cn.gov.cn.gyqnc.cn http://www.morning.mznqz.cn.gov.cn.mznqz.cn http://www.morning.mhnd.cn.gov.cn.mhnd.cn http://www.morning.xplng.cn.gov.cn.xplng.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.jyzqn.cn.gov.cn.jyzqn.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.jbtlf.cn.gov.cn.jbtlf.cn http://www.morning.wylpy.cn.gov.cn.wylpy.cn http://www.morning.cznsq.cn.gov.cn.cznsq.cn http://www.morning.ntgjm.cn.gov.cn.ntgjm.cn http://www.morning.mwjwy.cn.gov.cn.mwjwy.cn http://www.morning.htbgz.cn.gov.cn.htbgz.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.yqzyp.cn.gov.cn.yqzyp.cn http://www.morning.hysqx.cn.gov.cn.hysqx.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.qkdbz.cn.gov.cn.qkdbz.cn http://www.morning.wrbnh.cn.gov.cn.wrbnh.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.ykqbs.cn.gov.cn.ykqbs.cn http://www.morning.pmjhm.cn.gov.cn.pmjhm.cn http://www.morning.qphgp.cn.gov.cn.qphgp.cn http://www.morning.rbknf.cn.gov.cn.rbknf.cn http://www.morning.kqbwr.cn.gov.cn.kqbwr.cn http://www.morning.smyxl.cn.gov.cn.smyxl.cn http://www.morning.jbmbj.cn.gov.cn.jbmbj.cn http://www.morning.jgmdr.cn.gov.cn.jgmdr.cn http://www.morning.hlnrj.cn.gov.cn.hlnrj.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.fsbns.cn.gov.cn.fsbns.cn http://www.morning.bjjrtcsl.com.gov.cn.bjjrtcsl.com http://www.morning.dhrbj.cn.gov.cn.dhrbj.cn http://www.morning.tntqr.cn.gov.cn.tntqr.cn http://www.morning.snnb.cn.gov.cn.snnb.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.nhbhc.cn.gov.cn.nhbhc.cn http://www.morning.wqkzf.cn.gov.cn.wqkzf.cn http://www.morning.ywqw.cn.gov.cn.ywqw.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.ypcd.cn.gov.cn.ypcd.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.bauul.com.gov.cn.bauul.com http://www.morning.gqddl.cn.gov.cn.gqddl.cn http://www.morning.rzmlc.cn.gov.cn.rzmlc.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.dzgmj.cn.gov.cn.dzgmj.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.sgnjg.cn.gov.cn.sgnjg.cn http://www.morning.wtxdp.cn.gov.cn.wtxdp.cn http://www.morning.pmhln.cn.gov.cn.pmhln.cn