当前位置:网站首页>数组 旋转数组
数组 旋转数组
2022-06-10 20:50:00 【Morris_】
LC 旋转数组
思路:将末尾的元素移动到首位,其他位置的元素向后移动一位,循环k次
- Swift
/* 例: nums = [1,2,3], k = 4 [3,1,2] [2,3,1] [1,2,3] [3,1,2] 每次将最后以为元素移动到首位,其他位元素依次向后移动一位;循环移动k次 */
func rotate(_ nums: inout [Int], _ k: Int) {
var k: Int = k
while k > 0 {
let temp: Int = nums.last!
var index = nums.count - 1
while index > 0 {
nums[index] = nums[index - 1]
index -= 1
}
nums[0] = temp
k -= 1
}
}
边栏推荐
- Calculus review 1
- About type-C
- Which city should I go to after I graduate from it? Which position has a high salary? Which companies have good treatment?
- 【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas
- C language -- 7 operators
- H. Relationship among Nalu, RBSP and sodb in 264
- 旋转菜单3.0
- Has the samesite cookie problem occurred when using identityserver?
- 【MySQL】表的约束
- Quick start to VISSIM simulation
猜你喜欢

Qingniao Changping campus of Peking University: can I learn UI with a high school degree?

Which city should I go to after I graduate from it? Which position has a high salary? Which companies have good treatment?

【MySQL】錶數據的增删查改(DML)

As a programmer, is it really that important for the underlying principles?

Interview Essentials - basic knowledge of synchronized underlying principles

If else is too easy to use? (understanding this article will further improve your logic)

C language -- 7 operators

Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)

在模仿学习中进步的智能机器人

2022-06-09 RK817 PMU 电池温度检测
随机推荐
自媒体视频创作如何选择领域?
【MySQL】錶數據的增删查改(DML)
The process of keyword search in Oracle tables
Can I make up the exam if I fail the soft exam? Here comes the answer
目标检测相关概念的理解
Video monitoring system storage control, bandwidth calculation method
Abbexa cell free DNA kit instructions
Asynchronous, thread pool (completablefuture)
System reinstallation and system performance query
Anti shake and throttling
Signal and system review 1
Only three steps are needed to learn how to use low code thingjs to connect with Sen data Dix data
Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models
Principle of gravure overprint and factors affecting overprint
构建幼儿Steam教育实施策略
[qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
SQL Server row to column (pivot), column to row (unpivot)
[NK] question de calcul de 51 g pour le match lunaire Bullock
SQL第四练:字符串处理函数