当前位置:网站首页>Array rotates the array from bits of the specified length
Array rotates the array from bits of the specified length
2022-06-10 22:11:00 【Morris_】
Rotated array
Input : nums = [1,2,3,4,5,6,7], k = 3
Output : [5,6,7,1,2,3,4]
Bits from the specified length , Rotated array
Swift
var newArray: [Int] = []
let array: [Int] = [10, 20, 30, 40, 50, 60]
let k: Int = 3
for i in 0..<array.count {
let j: Int = (i + k) % array.count
print(j, array[j])
newArray.append(array[j])
}
print(array)
print(newArray)
Output is as follows :
3 40
4 50
5 60
0 10
1 20
2 30
[10, 20, 30, 40, 50, 60]
[40, 50, 60, 10, 20, 30]
newArray Is the rotated array
take array Replace all elements
var newArray: [Int] = []
var array: [Int] = [10, 20, 30, 40, 50, 60]
let k: Int = 3
for i in 0..<array.count {
let j: Int = (i + k) % array.count
print(j, array[j])
newArray.append(array[j])
}
array.replaceSubrange(0..<array.count, with: newArray)
print(array)
Optimize the implementation of the last interview , Take an intermediate array temp
var array: [Int] = [10, 20, 30, 40, 50, 60]
let temp: [Int] = array
let k: Int = 3
for i in 0..<array.count {
let j: Int = (i + k) % array.count
let ele = temp[j]
print(j, ele)
array.replaceSubrange(i..<i+1, with: [ele])
}
print(array)
The function is implemented as follows
func rotate(_ nums: inout [Int], _ k: Int) {
if k > 0 && k < nums.count {
let temp = nums
for i in 0..<nums.count {
nums.replaceSubrange(i..<i+1, with: [temp[(i+k) % nums.count]])
}
}
}
call
var nums: [Int] = [1,2,3,4,5,6,7,8]
let k: Int = 3
rotate(&nums, k)
print(nums)
Output
[4, 5, 6, 7, 8, 1, 2, 3]
边栏推荐
- 入行须知:运维需要懂编程吗?
- 【MySQL】錶數據的增删查改(DML)
- Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
- [generation confrontation network learning part I] classic Gan and its existing problems and related improvements
- Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
- Can I make up the exam if I fail the soft exam? Here comes the answer
- Abbexa cdan1 siRNA instruction manual
- 旋转菜单3.0
- [qingniaochangping campus of Peking University] the coordinated development of vocational education and general education, will this year's high school entrance examination be easy?
- Shell implements SSH login and executes commands
猜你喜欢

Acl2022 | bert2bert: an efficient pre training method of parameter reuse, which significantly reduces the training cost of oversized models

oc swift 混编

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

目标检测相关概念的理解

Forward slash "/", backslash "\," escape character "\" and file path separator cannot be clearly remembered

Icml2022 | sharp maml: model independent meta learning for sharpness perception

2022 - 06 - 09 rk817 PMU Battery Temperature Detection

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

北大青鸟昌平校区:高中学历可以学UI吗?

【Microsoft Azure 的1024种玩法】七十五.云端数据库迁移之快速将阿里云RDS SQL Server无缝迁移到Azure SQL Databas
随机推荐
磁盘序列号,磁盘ID,卷序列号的区别
SQL第四练:字符串处理函数
Shaping teenagers' comprehension ability with children's programming thinking
The process of keyword search in Oracle tables
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)
Extracting Nalu unit data from H264 real-time stream
C语言-排序中的快速排序(简称快排)
变量(自动变量、静态变量、寄存器变量、外部变量)与C的内存分配malloc/free、calloc/recalloc
数组 加一
C language ---9 first knowledge of macros and pointers
Abbexa 8-OHdG CLIA kit solution
Rotate navigation bar
Are you still writing the TS type code
Junior high school graduates who choose secondary vocational schools can also be promoted to institutions of higher learning
C language -- 10 first knowledge of structure
Mysql之将查询结果插入到其它表中
字符串 反转
The system needs to scan all the files and try to identify the cover of the video
Notes to entry: do I need to know programming for O & M?