LeetCode Rotate Array

YonBIP 3.0 报表开发+报表超链接 用友报表开发 阅读详情

LeetCode Rotate Array

Rotate Array Total Accepted: 5488 Total Submissions: 29811 My Submissions Question Solution 
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.


题目意思就是将数组按照指定的位置进行旋转,其中的Note说明至少有三种方法。

方法一:采用一个辅助空间,然后进行数据的拷贝,在此使用memcpy和memset等(面试有一些关于memcpy和memmove的题目,可以自行google),空间复杂度O(n)

void rotate(int nums[], int n, int k) 
{
	if (n <= 0 || k == 0)
		return;
	k > n ? k = k%n : n;
	const int intSize = sizeof(int);
	int * arr = new int[n];
	memset(arr, 0, n*intSize);
	memcpy(arr, nums, n*intSize);

	memcpy(nums, arr + (n-k), k*intSize);
	memcpy(nums + k, arr, (n - k)*intSize);

	delete[] arr;
}

方法二:每个数字移动k次,空间复杂度O(1),时间复杂度O(K*N)

for (int i = 0; i < k; i++)//每个数字移动k次
	{
		int tmp = nums[n - 1];
		for (int j = n - 1; j >= 0; j--)//后移数组元素
		{
			nums[j] = nums[j - 1];
		}
		nums[0] = tmp;
	}

方法三:根据规律旋转,时间复杂度和系统函数有关,空间复杂度O(0)

void rotate(int nums[], int n, int k) 
{
	reverse(nums, nums + n);//翻转整个数组[7,6,5,4,3,2,1]
	reverse(nums, nums + k%n);//翻转从起始位置到k%n位置处[5,6,7,4,3,2,1]
	reverse(nums + k%n, nums + n);//翻转剩余部分[5,6,7,1,2,3,4]
	
}

Gromacs 模拟昆虫 P450 解毒酶与农药结合完整指南,仅供参考! 🏆 本文收录于《全栈Bug调优(实战版)》专栏,致力于分享我在项目实战过程中遇到的各类Bug及其原因,并提供切实有效的解决方案。无论你是初学者还是经验丰富的开发者,本文将为你指引出一条更高效的Bug修复之路,助你早日登顶,迈向财富自由的梦想🚀!同时,欢迎大家关注、收藏、订阅本专栏,更多精彩内容正在持续更新中。让我们一起进步,Up!Up!Up!    备注: 部分问题/难题源自互联网,经过精心筛选和整理,结合数位十多年大厂实战经验资深大佬经验总结所得,数条可行方案供所需之人参考。 阅读详情

相关推荐

Qwen-Image LoRA训练实战:从数据准备到异常修复的全流程解析

本文详细解析了Qwen-Image LoRA训练的全流程,从数据准备到参数优化再到异常修复。通过实战案例展示了如何利用LoRA技术微调Qwen-Image模型,提升中文场景下的图像生成质量,特别针对手脚生成异常等问题提供了有效解决方案。文章还分享了中文提示词优化技巧和高级训练方法,帮助开发者高效训练出高质量的LoRA模型。

lemon的博客 965

leetcode 189. Rotate Array

leetcode 189. Rotate Array 调库: class Solution { public: void rotate(vector<int>& nums, int k) { int len=nums.size(); k=k%len; vector<int> vec_a(nums.end()-k,nums.end()); vector<int> vec_b(nums

Alfheim 139

深度学习中 Internal Covariate Shift 问题以及 Batch Normalization 的作用

深度学习中 Internal Covariate Shift 问题以及 Batch Normalization 的作用前言一、Batch Normalization是什么?1.1 Internal Covariate Shift1.2 Internal Covariate Shift 带来的影响1.3 如何减缓 Internal Covariate Shift 问题带来的影响白化(Whitening)白化存在的问题二、Batch Normalization2.1 传统 Normalization2.2 改进

一扣解千愁的博客 4296

LeetCode189 Rotate Array

LeetCode189 Rotate Array 最近重新开始刷题了! 因为之前写过的错题总是一错再错,想找个地方总结一下错题,方便之后回忆思路! 题目 Link: https://leetcode.com/problems/rotate-array/ Given an array, rotate the array to the right by k steps, where k is non-negative. Example1: Input: nums = [1,2,3,4,5,6,7], k =

weixin_42905061的博客 204

SAP中贸易伙伴的应用原理简析

公司也称为贸易伙伴,是出具合并报表的组织单位,可以为客户和供应商分配公司(贸易伙伴),用于识别集团内关闻公司间的业务往来,在编制合并报表时进行往来账务的计算处理。 如下图某贸易伙伴公司(关联方名称G0059)在我方公司的SAP系统中有多重身份(即是供应商,又是客户)。那么在我方公司SAP系统中建立起公司(贸易伙伴),并关联到供应商和客户主数据中。之后,就可以通过G0059这个贸易伙伴名称来识别与该贸易伙伴间的往来业务。 相应的配置设定如下: 定义公司(贸易伙伴或关联方) 会计凭证中会

专注SAP应用技术文章写作,致力于拉近用户与系统间的距离; 9814

QT编程系列1--QT学习前需知

本周学习了一些QT编程方面的知识,学写了几个程序,这里把记录下来。一方面对前面学习的知识做一个总结,便于以后复习、再学习和应用;另一方面也和大家做个交流,有不足的地方请各位看官多指点。   首先是做一些学习之前的交代:一,学习QT需要哪些基础;二,学习QT有哪些用途,QT较其他的开发工具有哪些特点;三,学习QT的思路。

霜月孤鸟&RTEMS&TinyOS 1115

leetcode (Rotate Array)

Title:Rotate Array    189 Difficulty:Easy 原题leetcode地址:https://leetcode.com/problems/rotate-array/   1. 详细讲解见代码注释,时间&amp;空间复杂度如下: 时间复杂度:O(n),最长需要遍历整个数组的长度。 空间复杂度:O(1),没有额外申请空间。 /** * ...

hsx1612727380的专栏 182

YOLO26涨点改进 | 全网独家,注意力创新改进篇 | Arxiv2025 | YOLO26引入MSLA多尺度线性注意力模块,高效提取多尺度特征和建模长程依赖,含多种创新改进,助力有效涨点

本文提出了一种多尺度线性注意力模块(MSLA)来优化YOLO26模型。MSLA通过并行使用不同尺寸的深度可分离卷积(3×3至9×9)提取多尺度特征,并结合线性注意力机制高效建模全局依赖关系。该模块显著提升了模型对多尺度目标的检测能力,同时保持了轻量化特性。实验表明,MSLA能有效增强小目标检测和复杂场景下的分割精度。文章详细介绍了MSLA的网络结构、核心优势及实现代码,并提供了两种集成MSLA的YOLO26改进方案,为实时目标检测任务提供了高效解决方案。

Ai缝合怪、助力小伙伴高效发论文 292

[LeetCode]189. Rotate Array

189. Rotate Array 一、题目 Problem Description: Given an array, rotate the array to the right by k steps, where k is non-negative. Follow up: Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. Could you do i

KKKKKKOBE_24的博客 199

GStreamer —— 2.1、Windows搭建GStreamer环境,Qt加载GStreamer库后运行 - “教程1:Hello world!“(附:完整源码)

GStreamer —— 2、Windows搭建GStreamer环境,Qt加载GStreamer库后运行"教程1:Hello world!"

796

GitLab CI/CD .gitlab-ci.yml 关键词(十一):发布版本 release

本篇文章介绍使用GitLab CI/CD 发布一个版本,对外发布。并填写一些发布的信息。

知无涯 4717

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 solu

新博客地址:www.yizhen-blog.com 欢迎交流 409

【Python】秋招机考 —— 2025/9/17 华为 AI 方向

秋招机考,华为 AI,使用 Python 完成编程。

一杯水果茶!足矣~ 1163

Leetcode之Rotate Array

题目: Given an array, rotate the array to the right byksteps, wherekis non-negative. Example 1: 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:...

qq_35455503的博客 225

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

leetcode13 Rotate Array

Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: 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,...

墨笙弘一 280

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

[leetcode]rotate-array 轮转数组

【代码】[leetcode]rotate-array 轮转数组。

xiaocong1990的博客 329

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

[勇者闯LeetCode] 189. Rotate Array

[勇者闯LeetCode] 189. Rotate ArrayDescription 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]. Info

fanguzhai的博客 271

记录下自己拙计的算法之旅 LeetCode Rotate Array

记录下自己拙计的算法之旅LeetCode Rotate Arraydef rotate(nums, k) num_cp = nums.clone pos = k % nums.length start_pos = 0 nums.length.times do nums[pos] = num_cp[start_pos] start_pos += 1

kevinzhou93的专栏 453

LeetCode: 189. Rotate Array

LeetCode: 189. Rotate Array 题目描述 Given an array, rotate the array to the right by k steps, where k is non-negative. Example 1: Input: [1,2,3,4,5,6,7] and k = 3 Output: [5,6,7,1,2,3,4] Explanat...

杨领well的专栏 126

[LeetCode] Rotate Array

Rotate an array of n elements to the right by k steps.For example, with n=7n = 7 and k=3k = 3, the array [1,2,3,4,5,6,7][1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4][5,6,7,1,2,3,4].Note: Try to come u

楚兴 3707

LeetCode 189 Rotate Array题解

题目地址:https://leetcode.com/problems/rotate-array/ 题目: Rotate an array of n elements to the right by k steps. 将一个具有n个元素的数组向右移动k个位置。 For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,

赞美月亮的专栏 774

LeetCode之Rotate Array

1、题目 Rotate an array ofnelements to the right byksteps. For example, withn= 7 andk= 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 solu

码莎拉蒂 2769

【Leetcode】Rotate Array

题目链接:https://leetcode.com/problems/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

业精于勤,荒于嬉 445

leetcode Rotate Array

题目连接 https://leetcode.com/problems/rotate-array/ Rotate Array Description 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...

dong12276的博客 162

LeetCode: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]. 思路分析:将数组分为左右两部分,先将左右两部分各自翻转,最后再将整个

yao_hust_iAC的专栏 1370
上一篇: 跟我一起学习VIM - The Life Changing Editor
下一篇: LeetCode Repeated DNA Sequences
FightForProgrammer
博客等级 码龄13年 239粉丝 75原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值