leetcode 将一个二维矩阵进行90度旋转
import numpy as np
import math
if __name__ == '__main__':
def rotate(matrix):
n = len(matrix[0])
for i in range(math.ceil((n-1)/2)):
for j in range(i,n-i-1):
temp = matrix[i][j]
matrix[i][j] = matrix[n-1-j][i]
matrix[n-1-j][i] = matrix[n-1-i][n-1-j]
matrix[n-1-i][n-1-j] = matrix[j][n-1-i]
matrix[j][n-1-i] = temp
return matrix
matrix =np.arange(1,17).reshape(4,4)
print(rotate(matrix))
如图3×3的旋转过程:

如图4×4旋转过程:

leetcode 将一个二维矩阵进行90度旋转的更多相关文章
- python3--算法基础:二维数组转90度
python3--算法基础:二维数组转90度 [0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3][0, 1, 2, 3] 二维数组转90度 [0, 0, 0, 0][1, 1, ...
- LeetCode——Rotate Image(二维数组顺时针旋转90度)
问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...
- python-二维数组实现90度旋转
本篇主要介绍了对一个N*N的数组,如果进行90度的旋转 首先,定义一个一维数组很简单,如下: a = [i for i in range(10)] print(a) -----结果----- 0, 1 ...
- LeetCode 搜索二维矩阵 II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...
- [LeetCode] Search a 2D Matrix II 搜索一个二维矩阵之二
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- [LeetCode] Search a 2D Matrix 搜索一个二维矩阵
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...
- LeetCode:搜索二维矩阵【74】
LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
随机推荐
- Linux系统chmod 777 误操作目录权限 - 恢复方法
小白操作Linux,手抖导致误修改了系统文件和目录权限,导致系统宕机的修复. -R / -R / test 有的是真不懂,执行了上面的第一条命令,有的是懂,但是操作太快或者粗心大意,或者有乱敲空格的恶 ...
- list 对像排序
在C#的List操作中,针对List对象集合的排序我们可以使用OrderBy.OrderByDescending.ThenBy.ThenByDescending等方法按照特定的对象属性进行排序,其中O ...
- 001-官网安装openstack之-安装前基础环境准备
0.安装常用软件包(根据个人习惯安装需要的软件包) [root@localhost ~]# yum -y install wget vim ntp net-tools tree openssh 1.配 ...
- zhengrui集训D1-D5笔记
Day_1 计数 它咕掉了 Day_1 序列数据结构 它咕掉了 Day_2 线性代数 高斯消元\Large{高斯消元}高斯消元 普通版:略 模质数:求逆 模合数:exgcd 逆矩阵\Large{逆矩阵 ...
- Luogu P4270 [USACO18FEB]Cow Gymnasts (打表找规律)
题意 传送门 题解 首先我们不竖着看奶牛而是横着看.从下往上把奶牛叫做处于第0,1,2...0,1,2...0,1,2...层.那么相当于第000层的不动,第111层的平移一格,第222层的平移222 ...
- How to find First Non-Repeated Character from String
You need to write a function, which will accept a String and return first non-repeated character, fo ...
- JavaScript有趣的知识点
JavaScript中总有一些有趣的小知识,而且又是很容易犯错的.我把我遇到的慢慢罗列一下,方便大家避坑 typeof(null)返回的结果是 object " "变成布尔类型为t ...
- List 中 forEach 的用法
1.forEach List list = new ArrayList<String>(); list.add("small"); list.add("sun ...
- 删除harbor项目下的所有镜像
user= pswd= url= proid= REPOS=$(curl -s -X GET --header 'Accept: application/json' "${url}/api/ ...
- PHP mysqli_close() 函数
关闭先前打开的数据库连接: <?php $con=mysqli_connect("localhost","my_user","my_passwo ...