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度旋转的更多相关文章

  1. 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, ...

  2. LeetCode——Rotate Image(二维数组顺时针旋转90度)

      问题: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockw ...

  3. python-二维数组实现90度旋转

    本篇主要介绍了对一个N*N的数组,如果进行90度的旋转 首先,定义一个一维数组很简单,如下: a = [i for i in range(10)] print(a) -----结果----- 0, 1 ...

  4. LeetCode 搜索二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. Python算法之动态规划(Dynamic Programming)解析:二维矩阵中的醉汉(魔改版leetcode出界的路径数)

    原文转载自「刘悦的技术博客」https://v3u.cn/a_id_168 现在很多互联网企业学聪明了,知道应聘者有目的性的刷Leetcode原题,用来应付算法题面试,所以开始对这些题进行" ...

  6. [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 ...

  7. [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 ...

  8. LeetCode:搜索二维矩阵【74】

    LeetCode:搜索二维矩阵[74] 题目描述 编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值.该矩阵具有如下特性: 每行中的整数从左到右按升序排列. 每行的第一个整数大于前一行的 ...

  9. Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)

    Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...

随机推荐

  1. 用python计算最高投标限价

    题目是文绉绉的说法,背景来于群里提问,是一份文件里面关于最高投标限价的确定. 最高投标限价下浮率在开标前在开标现场采取逐标段摇珠方式确定,摇珠操作办法如下:在下浮率摇珠范围内,以0.1%为以一档次增序 ...

  2. setuptools 版本太旧

    第二是 setuptools 版本太旧,所以出现以下问题Command "python setup.py egg_info" failed with error code 1 in ...

  3. oracle 12c 安装补丁报错

    环境: centos 7.5 oracle 12.2.0.1 报错: 执行一下报错: 更新dba_registry_sqlpatch $ORACLE_HOME/OPatch/datapatch -ve ...

  4. lvs+keepalived做高可用方案1

    本文我们主要讲解的是LVS通过keepalived来实现负载均衡和高可用,而不是我们第三篇文章介绍的通过手动的方式来进行配置.通过脚本的方式来显示RS节点的健康检查和LVS的故障切换.此文会通过一个实 ...

  5. 使用selenium实现站长素材图片采集

    from selenium import webdriver import requests,os from lxml import etree from selenium.webdriver.chr ...

  6. 解决每次执行Java等命令时都要重新source /etc/profile后才能执行,否则找不到命令

    linux mint 我们通常将环境变量设置在/etc/profile这个文件中,这个文件是全局的. /etc/profile:在登录时,操作系 统定制用户环境时使用的第一个文件 ,此文件为系统的每个 ...

  7. [HTML5] Add Semantic Styling to the Current Page of a Navigation Item with aria-current

    In this lesson, we are going to use aria-current to give a screen reader user more context about wha ...

  8. JQuery 实践---创建元素包装集

    1. 利用选择器,选择将被JQuery包装的元素 标识和选择DOM元素.JQuery采用我们已经知道的CSS语法并且扩展了一些.为了利用JQuery来选择元素,请把选择器包装在$()中. 基本CSS选 ...

  9. Java进阶知识18 Spring对象依赖关系的几种写法

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. 【概率论】3-6:条件分布(Conditional Distributions Part II)

    title: [概率论]3-6:条件分布(Conditional Distributions Part II) categories: Mathematic Probability keywords: ...