Java for LeetCode 048 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?

解题思路:

找规律即可,JAVA实现如下:

static public void rotate(int[][] matrix) {
	int[][] matrixArray=new int[matrix.length][matrix[0].length];
	for(int i=0;i<matrix.length;i++)
		System.arraycopy(matrix[i], 0, matrixArray[i], 0, matrix[i].length);
    for(int i=0;i<matrix.length;i++)
    	for(int j=0;j<matrix.length;j++)
    		matrix[i][j]=matrixArray[matrix.length-j-1][i];
    }
时间: 2024-08-03 19:04:08

Java for LeetCode 048 Rotate Image的相关文章

[Leetcode][048] Rotate Image 略详细 (Java)

题目在这里 https://leetcode.com/problems/rotate-image/ [个人分析] 这个题目,我觉得就是考察基本功.考察细心的,算法方面没有太多东西,但是对于坐标的使用有较高要求. 放了两个版本的答案,第一个版本是自己写的,第二个是目前最佳答案的Java改写. [代码注释] Solution1: 思路比较直接.既然要求in-place,那就在修改之前,先保存原先在那个位置上的值,然后尾巴咬尾巴的去修改:大意可以参考下图 Solution2: 偏数学的方法,先把矩阵“

Java for LeetCode 061 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. 解题思路: 只需找到对应的位置,然后指向head,接着把之前节点指向null即可,注意k可以取大于length的值,所以k%=len

Java for 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]. 解题思路: JAVA实现如下: public void rotate(int[] nums, int k) { k%=nums.length; k=nums.length-k; int[] nums2=ne

LeetCode 048 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? 代码如下: class Solution { public: void rotate(vector<vector<int> > &matrix) { const in

LeetCode:Rotate Array

1.题目名称 Rotate Array(平移数组) 2.题目地址 https://leetcode.com/problems/rotate-array/ 3.题目内容 英文:Rotate an array of n elements to the right by k steps. 中文:将一个长度为n的数组,向右平移k个单位 4.解题方法1 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

[LeetCode] 61. Rotate List 旋转链表

Given a linked list, rotate the list to the right by k places, where k is non-negative. Example 1: Input: 1->2->3->4->5->NULL, k = 2 Output: 4->5->1->2->3->NULL Explanation: rotate 1 steps to the right: 5->1->2->3-&g

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个节点放到前面,可以采用快慢指针处理.不

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i