问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3700 访问。

给定一个数组,将数组中的元素向右移动 个位置,其中 是非负数。

输入: [1,2,3,4,5,6,7] 和 k = 3

输出: [5,6,7,1,2,3,4]

解释:

向右旋转 1 步: [7,1,2,3,4,5,6]

向右旋转 2 步: [6,7,1,2,3,4,5]

向右旋转 3 步: [5,6,7,1,2,3,4]

输入: [-1,-100,3,99] 和 k = 2

输出: [3,99,-1,-100]

解释: 

向右旋转 1 步: [99,-1,-100,3]

向右旋转 2 步: [3,99,-1,-100]

说明:

  • 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。
  • 要求使用空间复杂度为 O(1) 的原地算法。

Given an array, rotate the array to the right by k steps, where k is non-negative.

Input: [1,2,3,4,5,6,7] and k = 3

Output: [5,6,7,1,2,3,4]

Explanation:

rotate 1 steps to the right: [7,1,2,3,4,5,6]

rotate 2 steps to the right: [6,7,1,2,3,4,5]

rotate 3 steps to the right: [5,6,7,1,2,3,4]

Input: [-1,-100,3,99] and k = 2

Output: [3,99,-1,-100]

Explanation: 

rotate 1 steps to the right: [99,-1,-100,3]

rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3700 访问。

public class Program {public static void Main(string[] args) {int[] nums = null;//[1,2,3,4,5,6,7] k = 3//第1次,反转尾部,1,2,3,4,7,6,5//第2次,反转头部,4,3,2,1,7,6,5//第3次,反转所有,5,6,7,1,2,3,4nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };Rotate(nums, 3);ShowArray(nums);nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };Rotate2(nums, 3);ShowArray(nums);nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };Rotate3(nums, 3);ShowArray(nums);Console.ReadKey();}private static void ShowArray(int[] array) {foreach(var num in array) {Console.Write($"{num} ");}Console.WriteLine();}private static void Rotate(int[] nums, int k) {int[] newNums = new int[k];int length = nums.Length;k %= length;int mid = length - k;for(int i = 0; i < k; i++) {newNums[i] = nums[mid + i];}for(int i = mid - 1; i >= 0; i--) {nums[i + k] = nums[i];}for(int i = 0; i < k; i++) {nums[i] = newNums[i];}}private static void Reverse(int[] array, int index, int length) {for(int i = index, count = -1; i < index + length / 2; i++) {count++;int t = array[i];array[i] = array[index + length - count - 1];array[index + length - count - 1] = t;}}private static void Rotate2(int[] nums, int k) {if(nums.Length == 1 || k == 0) {return;}k %= nums.Length;Reverse(nums, nums.Length - k, k);Reverse(nums, 0, nums.Length - k);Reverse(nums, 0, nums.Length);}private static void Rotate3(int[] nums, int k) {if(nums.Length == 1 || k == 0) {return;}k %= nums.Length;Array.Reverse(nums, nums.Length - k, k);Array.Reverse(nums, 0, nums.Length - k);Array.Reverse(nums);}}

以上给出3种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3700 访问。

5 6 7 1 2 3 4
5 6 7 1 2 3 4
5 6 7 1 2 3 4

分析:

显而易见,Rotate和Rotate2的时间复杂度均为:  ,Rotate的空间复杂度为:  ,Rotate2的空间复杂度为:  。Rotate3的时间和空间复杂度基于运行时所使用的反转算法。

Rotate2的解法符合题目“原地打转”要求。

C#LeetCode刷题之#189-旋转数组(Rotate Array)相关推荐

  1. leetcode刷题 153.寻找旋转排序数组中的最小值

    题目分析: 解法一: 该题是用来寻找最小值,我们可以直接用数组求最小值的方法来进行求解,但是我们观察到此题数组是一个 旋转数组,只要除第一位外后面每一位比第一位小,那么它就是最小值,否则第一位就是最小 ...

  2. Leetcode刷题 33.搜索旋转排序数组

    分析: 首先看到这题的第一印象是题目很长,感觉很复杂,但仔细看下来之后发现题目中间一段对解题没有任何的帮助,重点在最后一句,大概意思就是在目标数组中找到目标数据,并传出对应位置,否则传出-1 清楚了目 ...

  3. leetcode刷题:求旋转有序数组的最小值

    题目: 分析:由题中所给的描述,不管数组如何旋转,我们的整个数组,其实总是真正有序的适用于二分查找法. 代码如下: #include <stdio.h> int findMin(int* ...

  4. Leetcode刷题33. 搜索旋转排序数组

    升序排列的整数数组 nums 在预先未知的某个点上进行了旋转(例如, [0,1,2,4,5,6,7] 经旋转后可能变为 [4,5,6,7,0,1,2] ). 请你在数组中搜索 target ,如果数组 ...

  5. leetcode刷题:搜索旋转排序数组

    题目: 分析:当我们将数组旋转的时候,就无法再保证数组是完全有序的了,但是我们可以判断出那块是有序的.然后在进行比较和查找 如下代码: class Solution { public:int sear ...

  6. Leetcode刷题 34.在排序数组中查找元素的第一个和最后一个位置

    解法1: class Solution { public:vector<int> searchRange(vector<int>& nums, int target) ...

  7. C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...

  8. C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...

  9. C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4042 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...

  10. C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...

最新文章

  1. Codeforces Round #506 (Div. 3)
  2. java登录界面_java实现登陆页面
  3. 程序人生:织梦dedecms后台/会员验证码关闭
  4. python快速检测视频跳过帧_使用Python实现跳帧截取视频帧
  5. (JAVA)格式化输出日期
  6. 读取web应用的properties文件方法
  7. Tiktok才是跨境卖家的未来?
  8. Matlab期货量化交易特征选取,【策略分享】Matlab量化交易策略源码分享
  9. matlab工具箱作用简介,Matlab各工具箱功能简介(部分)
  10. 基于深度学习的视觉 SLAM 综述
  11. SG90舵机驱动,有代码
  12. 如何在Mac上清理磁盘空间?
  13. Windows远程控制时,输入账号密码无法登录,提示“用户名或密码错误”问题(已解决)
  14. OTL/OCL/BTL/甲类/乙类/甲乙类
  15. 在linux开发板上显示图片,制作开发板的logo标签
  16. android多个module打包aar,Android 多 Module 合并打包 AAR
  17. Token登录验证(附图)
  18. 子进程及时知道父进程已经退出的最简单方案
  19. R语言结构方程模型(SEM)在生态学领域中的应用
  20. Hadoop集群(第9期)_MapReduce初级案例

热门文章

  1. 原码、反码、补码详述
  2. Python——Python3.6.0+Scrapy安装方法(总算没有bug了)
  3. 华为机试——合并表记录
  4. C/C++—— int main(int argc,char* argv[])讲解
  5. 三层架构dao service 表示层 115721935
  6. Oracle收购Sun
  7. iOS实现字符串动画
  8. nginx 修改 max open files limits
  9. HashMap 和 Hashtable 的同和不同
  10. 关于在大网段中拆出小网段地址