leetcode——189 Rotate Array Total(数组的翻转)

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 problem.

[show hint]

Hint:

Could you do it in-place with O(1) extra space?

空间复杂度O(1)

Hide Tags: Array

解题思路:

先将5以前的数据翻转得到的数组是[4,3,2,1,5,6,7]

再将5及以后的数据翻转得到的数组是[4,3,2,1,7,6,5]

再将整个数组翻转即得到[5,6,7,1,2,3,4]. (即为所求)

代码如下:

	public static void rotate(int[]nums,int k)
	{
		//获取数组长度
		int size=nums.length;
		/**
		 * 如果K>size,求其等效长度,起初会认为K<size,但是在leetcode的OJ测试的时候
		 * 会出现这种规律,如实加了下面这行等效长度代码。
		 */
		if (k>size)
		{
			k=k%size;
		}
		//翻转第K位(不包括第K位)之前的数
		reversal(nums,0,size-k-1);
		//翻转第k位(包括第K位)之后的数
		reversal(nums,size-k,size-1);
		//翻转整个数组
		reversal(nums,0,size-1);
	}

	public static void reversal(int[] nums,int i,int j)
	{
		//设置中间存储变量
		int temp=0;
		//两端往中间依次进行换位
		while (i<j&&i>=0)
		{
			temp=nums[j];
			nums[j]=nums[i];
			nums[i]=temp;
			i++;
			j--;
		}
	}
时间: 2024-10-09 03:51:16

leetcode——189 Rotate Array Total(数组的翻转)的相关文章

leetCode 189. Rotate Array 数组

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

[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 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 pro

LeetCode 189 Rotate Array(旋转数组)

翻译 通过K步将一个有着n个元素的数组旋转到右侧. 例如, 给定n = 7和k = 3,数组[1,2,3,4,5,6,7]会被旋转成[5,6,7,1,2,3,4]. 批注: 尽你可能尝试多种解决方案,这里至少存在3种不同的方式去解决这个问题. 原文 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 rotate

[LeetCode]12. 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 pro

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

&amp;lt;LeetCode OJ&amp;gt; 189. Rotate Array

189. Rotate Array Total Accepted: 55073 Total Submissions: 278176 Difficulty: Easy 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

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 一个比较易于理解的方法是新开辟一个与原数组等长的数组,循环考察原数组的元素,将它们放到新数组中平移后的位置上,最后再将新数组上的元素赋回原数组. 一段可以

189. Rotate Array【easy】

189. Rotate Array[easy] 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 differen