Rotate Image
来源:互联网 发布:java根据ip获取运营商 编辑:程序博客网 时间:2024/05/19 03:30
题目详情:https://leetcode.com/problems/rotate-image/description/
本题思路:把第一行元素赋值给倒数第一列元素,把第二行元素一次赋值给倒数第二列的各个位置,依次类推。
但是这样就会一个问题:从第二行开始最后的几个元素会被覆盖掉,比如:
矩阵[ [ 5, 1, 9,11],[ 2, 4, 8,10],[13, 3, 6, 7],[15,14,12,16] ]
,当第一行元素赋值给倒数第一列元素之后矩阵变为:[ [5,1,9,5],[2,4,8,1],[13,3,6,9],[15,14,12,11] ]
,原来最后一列的最后3个元素被个改变了,而这些在后边还会用到,因此先将这些元素保存下来。通过观察发现需要保存的元素为斜对角线以下的元素
# -*- coding:utf-8 -*-class Solution(object): def rotate(self, matrix): """ :type matrix: List[List[int]] :rtype: void Do not return anything, modify matrix in-place instead. """ row,length=0,len(matrix) i,saved=0,[] while i<length:#保存会覆盖掉的元素 saved.append(matrix[i][length-i:length]) i=i+1 i=0 while i<length: newRow=matrix[i][0:length-i]+saved[i]#拼接出原来的行元素的集合 j=0 while j<length: matrix[j][length-i-1]=newRow[j]#把行元素的集合赋值给列元素 j=j+1 i=i+1
阅读全文
0 0
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- Rotate Image
- n皇后问题—回溯法 C++实现
- hash冲突解决
- bzoj3625(NTT+多项式求逆+多项式开根)
- VMware vSphere Client 6.0 安装提示 vmwareRemoteConsole.dll 无法注册
- SESSION COOKIE详解
- Rotate Image
- 浅析MVP设计模式
- 使用Python定制词云
- LeetCode-Reverse Nodes in k-Group
- Ceph网络模块(3)——AsyncMessenger代码流程分析
- Codeforces Round #431 (Div. 2) 题解
- ORACLE实例讲太多大道理还不如真正实际操作一次的教程
- Pythonday04
- Eigen random 产生随机数