惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
A
About on SuperTechFans
量子位
B
Blog RSS Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
MongoDB | Blog
MongoDB | Blog
小众软件
小众软件
Blog — PlanetScale
Blog — PlanetScale
Microsoft Azure Blog
Microsoft Azure Blog
V
V2EX
Google DeepMind News
Google DeepMind News
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
U
Unit 42
D
DataBreaches.Net
博客园 - Franky
D
Docker
宝玉的分享
宝玉的分享
Y
Y Combinator Blog
月光博客
月光博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Hugging Face - Blog
Hugging Face - Blog

博客园 - Dabay

[Leetcode][Python]56: Merge Intervals [Leetcode][Python]55: Jump Game [Leetcode][Python]54: Spiral Matrix [Leetcode][Python]53: Maximum Subarray [Leetcode][Python]52: N-Queens II [Leetcode][Python]51: N-Queens [Leetcode][Python]50: Pow(x, n) [Leetcode][Python]49: Anagrams [Leetcode][Python]47: Permutations II [Leetcode][Python]46: Permutations [Leetcode][Python]45: Jump Game II [Leetcode][Python]44:Wildcard Matching [Leetcode][Python]43: Multiply Strings [Leetcode][Python]42: Trapping Rain Water [Leetcode][Python]41: First Missing Positive [Leetcode][Python]40: Combination Sum II [Leetcode][Python]39: Combination Sum [Leetcode][Python]19: Remove Nth Node From End of List [Leetcode][Python]37: Sudoku Solver
[Leetcode][Python]48: Rotate Image
Dabay · 2015-03-26 · via 博客园 - Dabay
# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'

48: Rotate Image
https://leetcode.com/problems/rotate-image/

You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Could you do this in-place?

=== Comments by Dabay===
画一个图,先按照左上到右下的斜线翻转,然后再按照竖对称轴翻转。
'''

class Solution:
# @param matrix, a list of lists of integers
# @return nothing (void), do not return anything, modify matrix in-place instead.
def rotate(self, matrix):
dimension = len(matrix)
for i in xrange(dimension):
for j in xrange(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
for i in xrange(dimension):
for j in xrange(dimension/2):
matrix[i][j], matrix[i][dimension-1-j] = matrix[i][dimension-1-j], matrix[i][j]

def main():
sol = Solution()
matrix = [
[1,2],
[3,4]
]
sol.rotate(matrix)
print matrix

if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)