48. Rotate Image

先按对角线对称图形,再水平对折。

class Solution {
public void rotate(int[][] matrix) {
//1.transpose
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < i; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
//flip the matrix horizontally
for(int i = 0; i < matrix.length; i++){
for(int j = 0; j < matrix[0].length / 2; j++){
int temp = matrix[i][j];
matrix[i][j] = matrix[i][matrix[0].length - 1 -j];
matrix[i][matrix[0].length - 1 -j] = temp;
}
}
}
}

54. Spiral Matrix

从右到左,从下到上的时候,注意保证不重复。

class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> res = new ArrayList<Integer>();
if(matrix == null || matrix.length == 0 || matrix[0].length == 0)
return res;
int rowBegin = 0, rowEnd = matrix.length - 1, colBegin = 0, colEnd = matrix[0].length - 1;
while(rowBegin <= rowEnd && colBegin <= colEnd){
//to right
for(int j = colBegin; j <= colEnd; j++) res.add(matrix[rowBegin][j]);
rowBegin++; for(int i = rowBegin; i <= rowEnd; i++) res.add(matrix[i][colEnd]);
colEnd--; for(int j = colEnd; j >= colBegin && rowBegin <= rowEnd; j--) res.add(matrix[rowEnd][j]);
rowEnd--; for(int i = rowEnd; i >= rowBegin && colBegin <= colEnd; i--) res.add(matrix[i][colBegin]);
colBegin++;
}
return res;
}
}

59. Spiral Matrix II

规则的放入数字,不需要第三四步判断是否超出边界。

class Solution {
public int[][] generateMatrix(int n) {
if(n == 0)
return null;
int[][] matrix = new int[n][n];
int rowBegin = 0, rowEnd = n - 1, colBegin = 0, colEnd = n - 1;
int count = 0;
while(rowBegin <= rowEnd && colBegin <= colEnd){
//right
for(int j = colBegin; j <= colEnd; j++)
matrix[rowBegin][j] = ++count;
rowBegin++; //down
for(int i = rowBegin; i <= rowEnd; i++)
matrix[i][colEnd] = ++count;
colEnd--; //left
for(int j = colEnd; j >= colBegin; j--)
matrix[rowEnd][j] = ++count;
rowEnd--; //up
for(int i = rowEnd; i >= rowBegin; i--)
matrix[i][colBegin] = ++count;
colBegin++;
}
return matrix;
}
}

11/8 <matrix> LC 48 54 59的更多相关文章

  1. leetcode@ [54/59] Spiral Matrix & Spiral Matrix II

    https://leetcode.com/problems/spiral-matrix/ Given a matrix of m x n elements (m rows, n columns), r ...

  2. 54. 59. Spiral Matrix

    1. Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral ...

  3. [LC] 48. Rotate Image

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

  4. Singleton设计模式 分类: 设计模式 2014-12-03 17:54 59人阅读 评论(0) 收藏

    实现方法: public class SingleTon<T> where T : class, new() {     protected SingleTon() { }     pri ...

  5. LeetCode矩阵题型

    以三角形遍历矩阵 ; i < matrix.size(); ++i) { ; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j] ...

  6. Go语言标准库之time

    Go语言标准库之time 时间的格式化和解析 格式化 Format Go语言和其他语言的时间格式化的方式不同,Go语言格式化的方式更直观,其他的语言一般是yyyy-mm-dd package main ...

  7. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  8. leetcode 54. Spiral Matrix 、59. Spiral Matrix II

    54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...

  9. Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

      1: /// <summary> 2: /// Write an algorithm such that if an element in an MxN matrix is 0, it ...

随机推荐

  1. VS2017项目升级 error LNK2005: "public: __thiscall ATL::CTime::

    我是将项目升级到从VS2012 升级VS2017, 报错如下 1>atlsd.lib(atltime.obj) : error LNK2005: "public: __thiscall ...

  2. D3力布图绘制--节点跑掉,单曲线弯曲问题记录

    D3力布图绘制中遇到的交互问题,频繁操作数据后,会出现节点跑掉和单曲线弯曲的问题 问题描述 在id指向都正常的情况下出现以下2种状况: 单曲线弯曲 节点跑掉 经排查,是数据重复导致的问题 线条也是一样 ...

  3. D3力布图绘制--基本方法

    本文主要结合案例记录使用D3.js绘制力布图的基本方法 样例显示 基本配置 this.force = d3.layout .force() .size([this.width, this.height ...

  4. DGCNN

    架构总览 模型的整体架构源于 WebQA 的参考论文 Dataset and Neural Recurrent Sequence Labeling Model for Open-Domain Fact ...

  5. mycat 白话配置

    1.server.xml <user name="root" defaultAccount="true"> <property name=&q ...

  6. Spring自动注入,类型注入、名称注入(两种方式)

    参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...

  7. jdbc:mysql:/// jdbc连接数据url简写方式

    正常情况下我们写jdbc连接本地mysql数据库的时候通常是这样写 jdbc:mysql:localhost:3306/数据库名 下面就是要提到的简单的方法 jdbc:mysql:///数据库名

  8. RFC的远程调用-异步

    接上篇RFC的远程调用-同步(https://www.cnblogs.com/BruceKing/p/11169930.html). TABLES:USR21. DATA:A TYPE USR21-P ...

  9. Enum.GetValues(),返回System.Array的一个实例

    Array enumData = Enum.GetValues(e.GetType()); Console.WriteLine("This enum has {0} members.&quo ...

  10. Python - 条件控制、循环语句 - 第十二天

    Python 条件控制.循环语句 end 关键字 关键字end可以用于将结果输出到同一行,或者在输出的末尾添加不同的字符,实例如下: Python 条件语句是通过一条或多条语句的执行结果(True 或 ...