问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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 访问。

  1. public class Program {
  2. public static void Main(string[] args) {
  3. int[] nums = null;
  4. //[1,2,3,4,5,6,7] k = 3
  5. //第1次,反转尾部,1,2,3,4,7,6,5
  6. //第2次,反转头部,4,3,2,1,7,6,5
  7. //第3次,反转所有,5,6,7,1,2,3,4
  8. nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  9. Rotate(nums, 3);
  10. ShowArray(nums);
  11. nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  12. Rotate2(nums, 3);
  13. ShowArray(nums);
  14. nums = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  15. Rotate3(nums, 3);
  16. ShowArray(nums);
  17. Console.ReadKey();
  18. }
  19. private static void ShowArray(int[] array) {
  20. foreach(var num in array) {
  21. Console.Write($"{num} ");
  22. }
  23. Console.WriteLine();
  24. }
  25. private static void Rotate(int[] nums, int k) {
  26. int[] newNums = new int[k];
  27. int length = nums.Length;
  28. k %= length;
  29. int mid = length - k;
  30. for(int i = 0; i < k; i++) {
  31. newNums[i] = nums[mid + i];
  32. }
  33. for(int i = mid - 1; i >= 0; i--) {
  34. nums[i + k] = nums[i];
  35. }
  36. for(int i = 0; i < k; i++) {
  37. nums[i] = newNums[i];
  38. }
  39. }
  40. private static void Reverse(int[] array, int index, int length) {
  41. for(int i = index, count = -1; i < index + length / 2; i++) {
  42. count++;
  43. int t = array[i];
  44. array[i] = array[index + length - count - 1];
  45. array[index + length - count - 1] = t;
  46. }
  47. }
  48. private static void Rotate2(int[] nums, int k) {
  49. if(nums.Length == 1 || k == 0) {
  50. return;
  51. }
  52. k %= nums.Length;
  53. Reverse(nums, nums.Length - k, k);
  54. Reverse(nums, 0, nums.Length - k);
  55. Reverse(nums, 0, nums.Length);
  56. }
  57. private static void Rotate3(int[] nums, int k) {
  58. if(nums.Length == 1 || k == 0) {
  59. return;
  60. }
  61. k %= nums.Length;
  62. Array.Reverse(nums, nums.Length - k, k);
  63. Array.Reverse(nums, 0, nums.Length - k);
  64. Array.Reverse(nums);
  65. }
  66. }

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

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

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

分析:

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

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

C#LeetCode刷题之#189-旋转数组(Rotate Array)的更多相关文章

  1. LeetCode 189. 旋转数组(Rotate Array)

    189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...

  2. LeetCode 189:旋转数组 Rotate Array

    公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...

  3. LeetCode刷题--26.删除排序数组中的重复项(简单)

    题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(1)额外空间的条件下完成. 示例 ...

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

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

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

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

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

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

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

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

  8. leetcode刷题-80.删除排序数组中的重复项 II

    题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...

  9. leetCode刷题(找到两个数组拼接后的中间数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. db2数据库创建删除主键约束和创建删除唯一键约束

    创建.删除唯一约束: db2 "alter table tabname add unique(colname)" db2 "alter table tabname dro ...

  2. 云原生时代高性能Java框架—Quarkus(一)

    --- Quarkus&GraalVM介绍.创建并启动第一个项目 Quarkus系列博文 Quarkus&GraalVM介绍.创建并启动第一个项目 构建Quarkus本地镜像.容器化部 ...

  3. ES6语法——Promise对象

    一.概念 Promise是异步编程的一种解决方案(解决回调地狱的问题),是一个能够获取异步操作信息的对象.Promise的内部保存着某个未来才会结束的事件(通常是一个异步操作) 二.特点 1.Prom ...

  4. C++语法小记---运算符重载

    运算符重载 运算符重载的本质也是对已有功能的扩展 运算符重载的本质就是函数重载,只是函数变成了 operator + 运算符 当成员函数和全局函数对运算符进行重载时,优先调用成员函数 运算符重载为成员 ...

  5. 扩展BSGS

    \(BSGS\) 求解\(a^x\equiv b\pmod p\),且\(a\)与\(p\)互质 由\(a^{φ(p)}\equiv1 \pmod p\)和\(a^0\equiv 1\pmod p\) ...

  6. vue-过渡动画和 第三方动画库导入,带图

    vue路由过渡动画 //用transition将路由出口包裹起来 <transition name="fade" mode="out-in"> &l ...

  7. DJANGO-天天生鲜项目从0到1-001-环境框架搭建

    本项目基于B站UP主‘神奇的老黄’的教学视频‘天天生鲜Django项目’,视频讲的非常好,推荐新手观看学习 https://www.bilibili.com/video/BV1vt41147K8?p= ...

  8. 在同一form表单中如何提交两个不同的action

    摘自:CSDN博客 原文链接地址:http://blog.csdn.net/huazhangena/article/details/7903955 有两种办法可以实现:1.针对一个action有多个提 ...

  9. Python爬虫开发与项目实战pdf电子书|网盘链接带提取码直接提取|

    Python爬虫开发与项目实战从基本的爬虫原理开始讲解,通过介绍Pthyon编程语言与HTML基础知识引领读者入门,之后根据当前风起云涌的云计算.大数据热潮,重点讲述了云计算的相关内容及其在爬虫中的应 ...

  10. Linux最常用的基本操作复习

    .ctrl + shift + = 放大终端字体 .ctrl + - 缩小终端字体 .ls 查看当前文件夹下的内容 .pwd 查看当前所在的文件夹 .cd 目录名 切换文件夹 .touch 如果文件不 ...