问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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,4 nums = 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 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. Java Properties集合基础解析

    Java Properties集合基础解析 本期学习的properties集合是项目中经常用到的操作 什么是Properties集合? java.util.Properties集合继承于Hashtab ...

  2. 【Nginx】如何使用Nginx实现MySQL数据库的负载均衡?看完我懂了!!

    写在前面 Nginx能够实现HTTP.HTTPS协议的负载均衡,也能够实现TCP协议的负载均衡.那么,问题来了,可不可以通过Nginx实现MySQL数据库的负载均衡呢?答案是:可以.接下来,就让我们一 ...

  3. 在ASP.NET中,<%= %>和<%# %>有什么区别

    asp.net中<%#%>出现在repeater gridview等控件中.用以绑定控件的datasource asp.net中<%%>的意思是 上运行c#或者vb代码,比如: ...

  4. Dicom文件基本操作

    官方文档 网址:https://github.com/fo-dicom/fo-dicom托管在github上. 官方例子 Dicom文件基本操作 var file = DicomFile.Open(@ ...

  5. vue学习(十五) 过滤器简单实用

    vue过滤器: 概念:vue.js允许你自定义过滤器可被用作一些常见文本的格式化.过滤器可以用在两个地方:插值表达式   v-bind表达式  由管道符指示 //过滤器调用时候的格式 {{ name ...

  6. C# File.Exists 判断系统文件,警惕32位和64位的差异

    今天在调试一个Winform程序,使用File.Exists 判断一个已经存在的驱动文件,程序一直返回false.因为驱动文件属于系统目录,心想难道是权限不够导致的?然后用管理员身份运行软件,依然返回 ...

  7. ES5---Proxy的理解的使用

    定义:Proxy原意为“代理”,在这可以理解为代理/拦截器的意思.Proxy在一个目标对象前放置了一个拦截,凡是外界对该对象的访问,都必须通过这层拦截,所以Proxy可以对外界的访问进行过滤和改写. ...

  8. 《Python编程初学者指南》高清PDF版|百度网盘免费下载|Python基础

    <Python编程初学者指南>|百度网盘免费下载| 提取码:03b1 内容简介 Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.Python可以用于很多的领域,从科学计 ...

  9. Qt自定义控件之仪表盘1--简单的贴图仪表盘

    0.前言 学程序首先要输出hell world,学电子要先来个流水灯.学Qt,那就必须先来个自定义控件,若有人问我从哪个下手,我推荐仪表盘,可简可繁,从低配到高配齐全,可入门也可进阶. 1.仪表盘解析 ...

  10. 获取全部 txt 文本中出现次数最多的前N个词汇

    1.使用 chain 对 allwords 二维列表进行解包 from itertools import chain allwords = [] allwords.append(列表) 解包: cha ...