leetCode 48.Rotate Image (旋转图像) 解题思路和方法

Rotate Image

You are given an n x n 2D matrix representing an image.

Rotate the image by 90 degrees (clockwise).

Follow up:

Could you do this in-place?

思路:其实就是旋转数组,没有什么难度,代码如下:

public class Solution {
    public void rotate(int[][] matrix) {
    	int[][] a = new int[matrix.length][matrix.length];
    	//实现深拷贝
    	for(int i = 0; i < matrix.length; i++){
    		for(int j = 0; j < matrix.length;j++){
    			a[i][j] = matrix[i][j];
    		}
    	}
    	//数据旋转
        for(int i = 0; i < a[0].length; i++){
            int k = 0;
            for(int j = a.length-1; j >=0; j--){
                matrix[i][k++] = a[j][i];
                //System.out.print(a[j][i] + " ");
            }
            //System.out.println("");
        }
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-07-30 09:04:42

leetCode 48.Rotate Image (旋转图像) 解题思路和方法的相关文章

leetCode 72.Edit Distance (编辑距离) 解题思路和方法

Edit Distance Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a char

leetCode 15. 3Sum (3个数) 解题思路和方法

3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The

[LeetCode] 234. Palindrome Linked List 解题思路

Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time and O(1) space? 问题:给定一个单向列表结构,判断它是不是回文的. 补充:是否可以在 O(n) 时间,O(1) 额外空间下完成? 解题思路: 对于数组,判断是否是回文很好办,只需要用两个指针,从两端往中间扫一下就可以判定. 对于单向列表,首先想到的是,将列表复制一份到数组中,然后用上面

[LeetCode] Minimum Size Subarray Sum 解题思路

Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

leetCode 42.Trapping Rain Water(凹槽的雨水) 解题思路和方法

Trapping Rain Water Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. For example, Given [0,1,0,2,1,0,1,3,2,1,2,1], return 6. The above elevation map

LeetCode 48. Rotate Image(旋转图像)

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 题目标签:Array 这道题目给了我们一个n * n的矩阵,让我们旋转图片.题目要求in-place,所以就不能用额外的空间了.一开始自己写了一个很原始的方法,结果虽然通过,但是速度太慢.只好去看看别人的方法,看完觉得,自己

leetCode 61.Rotate List (旋转链表) 解题思路和方法

Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. For example: Given 1->2->3->4->5->NULL and k = 2, return 4->5->1->2->3->NULL. 思路:题目很清晰,思路是先得到链表长度,再从头开始直到特定点,开始变换连接即可. 代码如下: /** * D

Leetcode 48 Rotate Image

You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). Follow up:Could you do this in-place? 思路:把矩阵看成是由多个正方形一层一层包围起来的.转动的时候就是转动正方形的边,只要把边上的每个数依次替换就好,四个方向也就是进行四次替换,依次对每一层进行替换. 总层数 = matrix.length/2,第i层从第mat