当前位置:网站首页>Algorithm --- 2D array mesh migration (kotlin)
Algorithm --- 2D array mesh migration (kotlin)
2022-07-23 08:24:00 【Xiaomi technology Android research and development caoxinyu】
subject
- Two dimensional grid migration
To give you one m That's ok n Two dimensional grid of columns grid And an integer k. You need to grid transfer k Time .
Every time 「 transfer 」 The operation will trigger the following activities :
be located grid[i][j] The element will be moved to grid[i][j + 1].
be located grid[i][n - 1] The element will be moved to grid[i + 1][0].
be located grid[m - 1][n - 1] The element will be moved to grid[0][0].
Please return k The final result after the migration operation Two dimensional meshes .
Example 1:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 1
Output :[[9,1,2],[3,4,5],[6,7,8]]
Example 2:

Input :grid = [[3,8,1,9],[19,7,2,5],[4,6,11,10],[12,0,21,13]], k = 4
Output :[[12,0,21,13],[3,8,1,9],[19,7,2,5],[4,6,11,10]]
Example 3:
Input :grid = [[1,2,3],[4,5,6],[7,8,9]], k = 9
Output :[[1,2,3],[4,5,6],[7,8,9]]
Tips :
m == grid.length
n == grid[i].length
1 <= m <= 50
1 <= n <= 50
-1000 <= grid[i][j] <= 1000
0 <= k <= 100
Solutions
Except for the first Every other bit is set to next Finally, set the last digit of the two digit array to the first digit
resolvent
fun shiftGrid(grid: Array<IntArray>, k: Int): List<List<Int>> {
val height = grid.size
var width = if (grid.isNotEmpty()) grid[0].size else 0
var next = 0
for (i in 1..k) {
grid.forEachIndexed {
index, ints ->
ints.forEachIndexed {
index2, i ->
//
if (index == 0 && index2 == 0) {
next = i
} else {
var temp = i
grid[index][index2] = next
next = temp
if (index == height - 1 && index2 == width - 1) {
grid[0][0] = next
}
}
}
}
}
var result = ArrayList<List<Int>>()
grid.forEach {
val element = ArrayList<Int>()
result.add(element)
it.forEach {
element.add(it)
}
}
return result
}
Method 2 Convert to a one-dimensional array
fun shiftGrid(grid: Array<IntArray>, k: Int): List<List<Int>> {
val heightT = grid.size
var widthT = if (grid.isNotEmpty()) grid[0].size else 0
var result = ArrayList<ArrayList<Int>>()
grid.forEach {
val element = ArrayList<Int>()
result.add(element)
it.forEach {
_ ->
element.add(0)
}
}
grid.forEachIndexed {
height, ints ->
ints.forEachIndexed {
width, i ->
var newIndex = (height * widthT + width + k) % (heightT * widthT)
result[newIndex / widthT][newIndex % widthT] = i
}
}
return result
}
summary
1. It can also be converted to a one-dimensional array
2. Pay attention to... During conversion
var newIndex = (height * widthT + width) + (k % (heightT * widthT))
It can't be written like this Because of this newIndex The array may be out of bounds
For example, a total of 60 A place He was in 59 Let him move 73 According to the above algorithm The new location is 59 + 13 Across the border
Unless you take another mold
var newIndex = ((height * widthT + width) + (k % (heightT * widthT))) % (heightT * widthT)
You should let him move first k Then touch
边栏推荐
- Several ways of QT thread exit
- MySQL uses SQL statements to query all data with a field value divided by 10 equal to 0
- 阿里云国际版账户收到账号风险通知,怎么办?
- 容器监控三剑客CAdvisor收集监控数据 + InfluxDB储存数据 + Granfana展示图表数据的简介
- js 正则删除span标签以及标签里面的内容
- There are 13 detailed methods for JMeter to view the response of the result tree!
- Come on, slide to the next little sister
- C language decimal number to binary number
- flink使用ListState实现KeyedState
- LC:剑指 Offer 39. 数组中出现次数超过一半的数字
猜你喜欢

Go 并发编程基础:什么是上下文

Genesis公链:夯实Web 3.0发展底座

Understand the interrupt system in STM32 in simple terms -- from principle to simple engineering examples -- nanny level tutorial

园区/厂区怎么实现wifi上网短信认证

轻松带你走进turtle绘图的大门

关于常见排序的稳定性

There are 13 detailed methods for JMeter to view the response of the result tree!

深入浅出地理解STM32中的中断系统——从原理到简单工程示例——保姆级教程

Internet traffic scheduling scheme

类和对象上案例
随机推荐
volatile有什么用
“蔚来杯“2022牛客暑期多校训练营1
Redis persistence operation (RDB, AOF)
技术干货 | 数据处理好难?来看MindSpore提供的解决思路!
关于常见排序的稳定性
What type of die cutting machine can be used for paper?
There are 13 detailed methods for JMeter to view the response of the result tree!
Flynk uses liststate to implement keyedstate
Redis profile
"Weilai Cup" 2022 Niuke summer multi school training camp 1
Networkx visualizes graphs
Typora设置标题自动添加序号
oh-my-zsh
Internet traffic scheduling scheme
Several ways of QT thread exit
什么时候使用UserCF,什么时候使用ItemCF?
Redis 事务学习有感
敏捷测试团队组织构成
Object prototype of typescript object extension__ proto__ And prototype
树以及二叉树的常用性质以及遍历