记得有道Amazon的OA题目,好像是给定一个矩阵,让把矩阵的每个元素向右shift一个位置。这道题之前没有好好自己想过。今天正好刷到了rotate matrix,所以正好一块想了。

思路是类似LeetCode Spiral Matrix:

  1. 假设矩阵为方阵
  2. 设置top, left, bot, right四个边界变量,然后从最外圈到最内圈一圈一圈的shift。
  3. 设定一个count,当count < total elements in matrix的时候进行shift
  4. 在每一圈开始的时候记录下来matrix[top][left],然后开始shift
    1. 从top到bot
    2. 从left到right
    3. 从bot到top
    4. 从right到left
      1. 在最后一条边的结果尝试更新新的matrix[top][left],这时候的top为旧的,而left已经更新过一次了, 我们要分为两种情况考虑
        1. count != totalElements - 1, 这时候我们要:
          1. matrix[top][left] 更新为tmp
          2. count++
          3. top++
          4. 进入下一圈
        2. 否则 count == totalElements - 1,也要分为两种情况
          1. totalElements为奇数,我们不改变matrix[top][left]
          2. totalElements为偶数,这时我们依然要更新一次matrix[top][left] = tmp
        3. 然后count++结束循环返回结果
  5. 假如给定矩阵不为方阵,则我们还要加入更多判断,比如剩下最后一行或者最后一列的时候不更新之类的。最后一行或者最后一列可以由bot - top 或者 right - left分别求出

Time Complexity - O(mn),Space Complexity - O(1),  in place。

public class Solution {
public void rotateMatrixByOne(int[][] matrix) {
if (matrix == null || matrix[0] == null) {
return;
}
int m = matrix.length;
int n = matrix[0].length;
int left = 0, top = 0, right = n - 1, bot = m - 1;
int count = 0;
int totalElements = m * n; while (count < totalElements) {
int tmp = matrix[top][left];
if (count < totalElements) {
for (int i = top; i < bot; i++) {
matrix[i][left] = matrix[i + 1][left];
count++;
}
left++;
}
if (count < totalElements) {
for (int i = left - 1; i < right; i++) {
matrix[bot][i] = matrix[bot][i + 1];
count++;
}
bot--;
}
if (count < totalElements) {
for (int i = bot + 1; i > top; i--) {
matrix[i][right] = matrix[i - 1][right];
count++;
}
right--;
}
if (count < totalElements) {
for (int i = right + 1; i > left; i--) {
matrix[top][i] = matrix[top][i - 1];
count++;
}
if (count != totalElements - 1) {
matrix[top][left] = tmp;
} else if (totalElements % 2 == 0) {
matrix[top][left] = tmp;
}
count++;
top++;
}
}
}
}

Rotate Matrix by One的更多相关文章

  1. [Leetcode] Template to rotate matrix

    Rotate the image by 90 degrees (clockwise). Given input matrix = [ [1,2,3,4], [5,6,7,8], [9,10,11,12 ...

  2. 旋转矩阵(Rotate Matrix)的性质分析

    博客转载自:http://www.cnblogs.com/caster99/p/4703033.html 学过矩阵理论或者线性代数的肯定知道正交矩阵(orthogonal matrix)是一个非常好的 ...

  3. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  4. [Leetcode][Python]48: Rotate Image

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 48: Rotate Imagehttps://leetcode.com/pr ...

  5. [LeetCode]Rotate Image(矩阵旋转)

    48. Rotate Image     Total Accepted: 69437 Total Submissions: 198781 Difficulty: Medium You are give ...

  6. LeetCode 48 Rotate Image(2D图像旋转问题)

    题目链接: https://leetcode.com/problems/rotate-image/?tab=Description   Problem:给定一个n*n的二维图片,将这个二维图片按照顺时 ...

  7. LeetCode解题报告—— Permutations & Permutations II & Rotate Image

    1. Permutations Given a collection of distinct numbers, return all possible permutations. For exampl ...

  8. [LeetCode] Rotate Image n-by-n矩阵顺时针旋转

    You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...

  9. C#LeetCode刷题之#48-旋转图像(Rotate Image)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3668 访问. 给定一个 n × n 的二维矩阵表示一个图像. 将 ...

随机推荐

  1. 立即执行函数(IIFE)的理解与运用

    作为JavaScript的常用语法,立即执行函数IIFE(Immediately-Invoked Function Expression)是值得我们认真去学习探究的. 一.创建函数的两种方式 我们先从 ...

  2. rapid js framework

    allcountjs.com http://mean.io/ https://www.meteor.com/ http://sailsjs.org/#!/ nodejs 博客 http://hexo. ...

  3. 用C++编写一个随机产生多个两位数四则运算式子的简单程序

    一 设计思想: 1.首先可以想到一个四则运算式子的组成:两个运算数和一个运算符: 2.两个运算数的随机由调用随机函数产生,其中可以设定运算数的范围: 3.一个运算符的随机产生可以分为加减乘除四种情况, ...

  4. 《梦断代码》读书笔记第0篇——“软件时间”、“死定了”、“Agenda之魂“

    第0章  软件时间 在未读这本书前,刚看到名字觉得是本讲代码的书,后来老师说是一个个的故事,这引起了我的兴趣,于是我便速速开始了第0章的阅读,读完一遍大概能读懂在讲什么,可能由于是译过来的书,书里面一 ...

  5. 关于js with语句的一些理解

    关于js with语句的一些理解   今天看到js的with语句部分,书中写到,with语句接收的对象会添加到作用域链的前端并在代码执行完之后移除.看到这里,我有两点疑问,添加到作用域链前端是不是指对 ...

  6. .NET-提取字符串实践总结

    前阶段工作时遇到一个截取字符串的问题,由于字符串比较长,大概得几万字符吧(XML形式),要提取中间两个节点之间的内容,在网上费了好大功夫才找到能用的正则.工作当用的时候碰到这样的事最蛋疼了,网上的资源 ...

  7. schedule和scheduleAtFixedRate区别

    需求: 由于系统长期运作,各设备之间产生很多信息,一段时间后需要清除数据 考虑方案: 用schedule还是scheduleAtFixedRate,在此比较分析了下这两个的区别 schedule和sc ...

  8. JS 学习笔记--8---Function类型

    练习使用的浏览器IE11   JS 中Function类型实际上是一种对象,每一个函数实际上都是Function类型的一个实例,每一个函数都有一些默认的属性和方法.由于函数是对象,故函数名实际上也是一 ...

  9. objc swift 混编

    原链接:http://blog.csdn.net/xuanwenchao/article/details/30226823 在xocde6出来我们大部分代码都是用objective-c写的(部分C/C ...

  10. 自定义对话框 提示:Unable to add window token null is not for an application

    这是因为在new Dialog(context);的时候传入的context是通过getApplicationContext()获得的,这样就会报错. 把context的获得方式改为MainActiv ...