如何计算nxn 2D阵列中连续数字的最大数量,并将其替换为另一个数字。 [英] How do I count the maximum number of contiguous numbers in a nxn 2D array and replace them with another number.

查看:78
本文介绍了如何计算nxn 2D阵列中连续数字的最大数量,并将其替换为另一个数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于nXn 2D数组,例如

For a nXn 2D array for example

12001231
12331212
12331212
12331212
12331212
12331212
12331212





如何计算最连续出现的元素?上面的2D数组应该具有3作为最多的连续出现数。用4替换它们。所以新数组应该是:



How do I calculate the element that appears contiguously most number of times? The above 2D array should has 3 as the most number of contiguous occurring number. Replace them with 4. So the new array should be:

12001231
12441212
12441212
12441212
12441212
12441212
12441212





我的尝试:



从[0] [0]开始,我尝试检查邻居(左,右,上,下)并传播。但我无法想到一个有效的逻辑。



What I have tried:

starting from [0][0], I tried checking the neighbors(left, right, up, down) and propagate. But am not able to think of an efficient logic for the same.

推荐答案

我会说'最大连续数等于位数 '。

创建一个数组,比如说 count [10] 来存储连续出现的数字之和 0..9 并在迭代输入数组时上传它。

然后找到计数最多的数字并将其替换为输入数组应该是琐碎的任务。
I would say 'max number of contiguous equal digits'.
Make an array, say count[10] to store the sum of contiguous occurrences of the digits 0..9 and uptade it's contet while iterating on the input array.
Then finding the digit with greatest count and replacing it into the input array should be a trivial task.


Quote:

但我无法想到相同的有效逻辑。

But am not able to think of an efficient logic for the same.



不要试图提高效率。首先,找一个工作算法。

一旦你得到一个解决方案,考虑优化。


Do not try to be efficient. First of all, find a working algorithm.
Once you got a solution, think about optimization.

引用:

从[0] [0]开始,我尝试检查邻居(左,右,上,下)并传播。

starting from [0][0], I tried checking the neighbors(left, right, up, down) and propagate.



这基本上是解决方案。

优化可以避免重复检查。


This is basically the solution.
An optimization can be to avoid duplicate checking.


这篇关于如何计算nxn 2D阵列中连续数字的最大数量,并将其替换为另一个数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆