当前位置:网站首页>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]
边栏推荐
- 数组 两数之和
- String analysis and use
- A WPF developed Print dialog box -printdialogx
- Only this is the most true reason why leaders promote you. The rest is nonsense!
- AI blessing real-time interaction | analysis of zegoavatar facial expression following technology
- 19 MySQL optimizations commonly used in projects
- 学IT毕业后该去哪个城市?哪个岗位薪资高?哪些公司待遇好?
- Explain in detail the arithmetic operators related to matrix operation in MATLAB (addition, subtraction, multiplication, division, point multiplication, point division, power)
- C中字符串查找
- C language ---6 first knowledge of selection statement, loop statement, function and array
猜你喜欢

C language -- 11 branch statement if else
![[Warning] TIMESTAMP with implicit DEFAULT value is deprecated](/img/e8/53c18a7944d160238f2f1c0f8f04b1.jpg)
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated

Mysql 什么是聚集索引和非聚集索引?

如何激发文化创新的活力和驱动力

C language -- 7 operators
![[Warning] TIMESTAMP with implicit DEFAULT value is deprecated](/img/e8/53c18a7944d160238f2f1c0f8f04b1.jpg)
[Warning] TIMESTAMP with implicit DEFAULT value is deprecated
PHP pseudo protocol implementation command execution details
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)

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

2022-06-09 RK817 PMU 电池温度检测
随机推荐
C language -- 3 variables for beginners
Part 7: Lesson 2 general skills of consultants - how to install and uninstall SAP ERP system client
旋转菜单2.0
Rotate menu 2.0
String analysis and use
Course design of imitation pottery ticket of wechat applet
Icml2022 | sharp maml: model independent meta learning for sharpness perception
Understanding of related concepts of target detection
2021年平均工资出炉,IT行业不出所料
一次SQL查询优化原理分析(900W+数据从17s到300ms)
数组 是否存在重复元素
Abbexa low sample size chicken lysozyme C (Lyz) ELISA Kit
防抖和节流
入行须知:运维需要懂编程吗?
数组 求并集
2022 - 06 - 09 rk817 PMU Battery Temperature Detection
数组 移动0
Install MySQL on Linux system. Problems encountered in xshell
构建幼儿Steam教育实施策略
C language -- 4 first-time constant