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 ...
随机推荐
- orchard 中文文档 中英对照版
ORCHARD CMS a free, open source, community-focused Content Management System built on the ASP.NET MV ...
- [算法模版]Tarjan爷爷的几种图论算法
[算法模版]Tarjan爷爷的几种图论算法 前言 Tarjan爷爷发明了很多图论算法,这些图论算法有很多相似之处(其中一个就是我都不会).这里会对这三种算法进行简单介绍. 定义 强连通(strongl ...
- ZooKeeper 入门看这篇就够了
什么是 ZooKeeper? ZooKeeper 是一个分布式的,开放源码的分布式应用程序协同服务.ZooKeeper 的设计目标是将那些复杂且容易出错的分布式一致性服务封装起来,构成一个高效可靠的原 ...
- Linux shell自动读mongo数据、远程获取文件大小示例脚本
1.示例1 功能:对mongoDB导出数据,根据sid的不同状态进行统计 技术点:shell bash 读写文件.字符串截取.函数.用多个文件提到的map.grep查找并赋值给变量 #!/bin/b ...
- 卡尔曼滤波C++代码
#include <ros/ros.h> #include <string> #include <stdlib.h> #include <iostream&g ...
- LeetCode 394:字符串解码 Decode String
题目: 给定一个经过编码的字符串,返回它解码后的字符串. Given an encoded string, return its decoded string. 编码规则为: k[encoded_st ...
- Linux安装部署jdk+tomcat+mysql
Linux安装部署测试环境 1. JDK安装下载 安装 yum search jdk 找到合适的jdk版本,以下图中的版本为例 yum install java-1.8.0-openjdk.x86_6 ...
- Python 文件readlines()方法
原文连接:https://www.runoob.com/python/file-readlines.html readlines()方法用于读取所有行(直到结束符EOF)并返回列表,该列表可以由pyt ...
- golang两种在for循环中使用goroutine的错误形式
1. 闭包中使用循环体中变化的量 platground链接: https://play.golang.org/p/6x6_tuQNjUO type Value struct{ val int } fu ...
- Java8新特性——新一套时间API的使用
JDK 1.0中包含了一个java.util.Date类,但是它的大多数方法已经在JDK 1.1引入Calendar类之后被弃用了.而Calendar并不比Date好多少.它们面临的问题是: 可变性: ...