C#LeetCode刷题之#189-旋转数组(Rotate Array)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3700 访问。
给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。
输入: [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)的更多相关文章
- LeetCode 189. 旋转数组(Rotate Array)
189. 旋转数组 LeetCode189. Rotate Array 题目描述 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输入: [1,2,3,4,5,6, ...
- LeetCode 189:旋转数组 Rotate Array
公众号:爱写bug(ID:icodebugs) 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. Given an array, rotate the array to the ...
- LeetCode刷题--26.删除排序数组中的重复项(简单)
题目描述 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度.不要使用额外的数组空间,你必须在原地修改输入数组并在使用O(1)额外空间的条件下完成. 示例 ...
- C#LeetCode刷题之#34-在排序数组中查找元素的第一个和最后一个位置(Find First and Last Position of Element in Sorted Array)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4970 访问. 给定一个按照升序排列的整数数组 nums,和一个目 ...
- C#LeetCode刷题之#350-两个数组的交集 II(Intersection of Two Arrays II)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4044 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- C#LeetCode刷题之#349-两个数组的交集(Intersection of Two Arrays)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4042 访问. 给定两个数组,编写一个函数来计算它们的交集. 输入 ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- leetcode刷题-80.删除排序数组中的重复项 II
题目 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成. ...
- leetCode刷题(找到两个数组拼接后的中间数)
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
随机推荐
- P.SDA1.DEV - 一个没有服务器的图床
图床特色 P.SDA1.DEV的愿景是为大家提供一个免费.长期稳定外链分享图片的选择. P.SDA1.DEV的主要特点有: 完全建构在Serverless云服务上,致力于提供(墙外)可用性99.9%的 ...
- JMS资源文件下载列表
网关程序(Gateway) https://files.cnblogs.com/files/IWings/Gateway.zip 网关裁判程序(GatewayReferee) https://file ...
- 一文带你学习DWS数据库用户权限设计与管理
前言 本文将介绍DWS基于RBAC(Role-Based Access Control,基于角色的访问控制)的数据库用户权限管理.简单地说,一个用户拥有若干角色,每一个角色拥有若干权限.这样,就构造成 ...
- Django开发之ORM批量操作
版本 1 Python 3.8.2 2 Django 3.0.6 批量入库 场景: 前端页面通过 textarea 文本框提交一列多行数据到Django后台,后台通过ORM做入库操作 表名: Tabl ...
- PHP入门之函数
前言 之前对PHP的类型.运算符和流程控制简单说了一下.想了解的,这是地址. PHP入门之类型与运算符 PHP入门之流程控制 下面对函数简单说一下. 函数的基本概念 为完成某一个功能的程序指令的合集, ...
- 第二部分_Mac技巧
原文是"池建强"的微信文章,公众号为"MacTalk" 第五十一天 mdfind是一个非常灵活的全局搜索命令,类似Spotlight的命令行模式,可以在任何目录 ...
- 3-Pandas之什么是Panel?
一.什么是Panel Series:包含一维索引的一组数据 DataFrame:包含index和columns两个轴 Panel(面板):一种三维数据容器 一个Panel对象由3个轴构成: items ...
- python小题目汇总
1.编程计算两个日期之间的天数与周数 Python时间处理---dateutil模块: dateutil模块主要有两个函数,parser和rrule. 其中parser是根据字符串解析成datetim ...
- PHP mt_rand() 函数
实例 生成随机数: <?phpecho(mt_rand() . "<br>");echo(mt_rand() . "<br>"); ...
- PHP mysqli_real_escape_string() 函数
转义字符串中的特殊字符: <?php 高佣联盟 www.cgewang.com // 假定数据库用户名:root,密码:123456,数据库:RUNOOB $con=mysqli_connect ...