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 server 按照字段分组 重新设置组序号
SELECT cpr.Id, cpr.CreateTime, cpr.Number FROM CarParkingRegistration cpr SELECT CONCAT(FORMAT(cpr ...
- UAC简介
用户帐户控制 (User Account Control) 是Windows Vista(及更高版本操作系统)中一组新的基础结构技术,可以帮助阻止恶意程序(有时也称为“恶意软件”)损坏系统,同时也可以 ...
- VBA基础 - 分支和循环
概要 编程语言的基础除了数据类型, 就是控制结构了. 所谓控制结构, 主要就是分支和循环. 分支 废话不说, 直接示例代码: 单条件判断 1 Sub Test() 2 If 2 > 1 Then ...
- 如何保障MySQL主从复制关系的稳定性?关键词(新特性、crash-safe)
一 前言 MySQL 主从架构已经被广泛应用,保障主从复制关系的稳定性是大家一直关注的焦点.MySQL 5.6 针对主从复制稳定性提供了新特性: slave 支持 crash-safe.该功能可以解决 ...
- 【洛谷5437】【XR-2】约定(拉格朗日插值)
[洛谷5437][XR-2]约定(拉格朗日插值) 题面 洛谷 题解 首先发现每条边除了边权之外都是等价的,所以可以考虑每一条边的出现次数. 显然钦定一条边之后构成生成树的方案数是\(2*n^{n-3} ...
- 转 tty 设备读写
转自https://feng-qi.github.io/2017/05/04/how-to-read-write-to-tty-device/ <p>这是 StackExchange 上的 ...
- Docker(Linux)学习笔记以及Redis/MariaDB的容器使用后台全自动启动
1:Docker安装,由于Docker后续pull镜像的服务器默认是在国外的,速度实在是太慢,这里使用阿里云的镜像 阿里云的Docker CE 镜像源站进行安装 docker ===========U ...
- Windows Form父子两个窗体之间的传值测试
1:先看测试的效果图: 2:全部的代码 using System; using System.Windows.Forms; namespace WindowsForms { public partia ...
- 邮箱图标的css样式
<div> <div style="position:relative; height:40px;width: 70px;border:2px solid black; m ...
- 在秉火STM32F429挑战者开发板上移植Huawei LiteOS指南
昨天在B站上突然看到了一个短视频,是在正点原子的战舰V3开发板上移植华为的Huawei LiteOS操作系统,就是这个视频:看完鸿蒙OS发布会,试用华为的物联网操作系统Lite OS(B站),于是呢, ...