Rotate Image

参照计算机图形学图形变换即可。

public class Solution {

    public void rotate(int[][] matrix) {
        if(matrix.length<=1)return ;
        float dip=(float) ((matrix.length-1)/2.0);
        int[][] res=new int[matrix.length][matrix.length];
        for(int i=0;i<matrix.length;i++)
            for(int j=0;j<matrix.length;j++)
            {
                float x=i-dip;
                float y=j-dip;
                float newX=y+dip;
                float newY=-x+dip;
            //    System.out.println(newX+"    "+newY);
                res[(int)newX][(int)newY]=matrix[i][j];
            }
        for(int i=0;i<matrix.length;i++)
            for(int j=0;j<matrix.length;j++)
                matrix[i][j]=res[i][j];
    }
}

时间: 2024-10-29 10:45:43

Rotate Image的相关文章

Rotate List leetcode java

题目: 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. 题解: 这道题主要先理解题意,就是倒着数k个node,从那开始到结尾和之前那部分对调,那个例子就是,4->5拿前面来,

Rotate Image leetcode java

题目: 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? 题解: 这道题就是考察很直白的旋转坐标.要in place的.画个图自己算算就出来了. 代码如下: 1  /*   public void rotate(int[][] matrix) { 2         in

Rotate List leetcode

这个题很有意思,看题目可以想到利用循环链表,将链表构成一个环后,旋转一定角度后,然后再拆开环,就可以得到新的链表 这里需要注意的就是k值和链表长度的关系,我们可以将环看做钟表,链表长度n就是这个钟表的最大刻度,k值是指针走过的刻度,k值可以比n小,也可以比n大,因为钟表环形循环的特点,指针可能已经绕了钟表好几周,这几周可以完全省略掉,只取余数k % n就是指针走过的有效刻度. 我们如何得到新的头结点呢,联想到钟表,我们假设原来头结点在0刻度上,旋转后,原来 -k%n 刻度来到了0刻度的位置上,只

Rotate Array leetcode

Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note:Try to come up as many solutions as you can, there are at least 3 different ways to solve this pro

Rotate Image &lt;leetcode&gt;

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? 思路:首先左右翻转,然后按照左下,右上对角线翻转,代码如下: 1 class Solution { 2 public: 3 void rotate(vector<vector<int> > &matri

Rotate List —— LeetCode

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. 题目大意:给一个链表和一个非负整数k,把链表向右循环移位k位. 解题思路:踩了个坑,k有可能比链表的长度还长,比如长度为3的链表,k=2

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

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. 这道题的要求是向右旋转链表k步. 其实就是把链表后面l-k个节点放到前面,可以采用快慢指针处理.不

[LeetCode]189.Rotate Array

题目 Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. Note: Try to come up as many solutions as you can, there are at least 3 different ways to solve this

leetcode 61 Rotate List ----- java

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.题目意思不难,就是说给一个数k,然后从右向左数第k个节点,然后以这个节点为开头,重新组成一个链表. 需要注意的就是如果k大于链表长度len