11/8 <matrix> LC 48 54 59
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的更多相关文章
- 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 ...
- 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 ...
- [LC] 48. Rotate Image
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- Singleton设计模式 分类: 设计模式 2014-12-03 17:54 59人阅读 评论(0) 收藏
实现方法: public class SingleTon<T> where T : class, new() { protected SingleTon() { } pri ...
- LeetCode矩阵题型
以三角形遍历矩阵 ; i < matrix.size(); ++i) { ; j < matrix[i].size(); ++j) swap(matrix[i][j], matrix[j] ...
- Go语言标准库之time
Go语言标准库之time 时间的格式化和解析 格式化 Format Go语言和其他语言的时间格式化的方式不同,Go语言格式化的方式更直观,其他的语言一般是yyyy-mm-dd package main ...
- 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 ...
- leetcode 54. Spiral Matrix 、59. Spiral Matrix II
54题是把二维数组安卓螺旋的顺序进行打印,59题是把1到n平方的数字按照螺旋的顺序进行放置 54. Spiral Matrix start表示的是每次一圈的开始,每次开始其实就是从(0,0).(1,1 ...
- 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 ...
随机推荐
- sql语句优化的30种方法
转载于:https://www.cnblogs.com/Little-Li/p/8031295.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的 ...
- centos 安装python3.7
先安装依赖包: yum -y install bzip2 bzip2-devel ncurses openssl openssl-devel openssl-static xz lzma xz-dev ...
- 论文阅读: v-charge项目: 电动车的自动泊车和充电
Abstract AVP服务会缓和电动车现有两个缺点: 有限的行驶范围和很长的充电时间. v-charge用相机和超声波在GPS-denied的区域全自动形式. 这篇paper叙述了下述几方面的优势: ...
- HTML+css基础 css选择器的种类
css选择器的种类 标签 权重是001 类 class权重是0010 相当于255个标签选择器 Id 权重是0100相当于255个类 *通配符 代表所有的标签 权重是0000 后代选 ...
- 也作一下装配脑袋的Expression习题【转】
一.习题 http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html 二.参考 http://msdn.micro ...
- WPF xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<Button Grid.Row="1" Content="Load Data" BorderBrush="Black" Border ...
- 读取txt文件内容,并按一定长度分页显示
private List<string> SaveContentUpload(FileUpload file) { List<string> list_content = ne ...
- C# - VS2019WinFrm程序通过SMTP方式实现邮件发送
前言 本篇主要记录:VS2019 WinFrm桌面应用程序通过SMTP方式实现邮件发送.作为Delphi转C#的关键一步,接下来将逐步实现Delphi中常用到的功能. 准备工作 搭建WinFrm前台界 ...
- Struts2框架简单介绍
如需,了解Struts2详情,请点击,传送门 工作原理 在Struts2 框架中的处理大概分为以下步骤: 1.客户端初始化一个指向servlet容器(例如Tomcat)的请求. 2.这个请求经过一系列 ...
- selenium简单使用
简介 Selenium是一个用于Web应用程序测试的工具.Selenium可以直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Fi ...