首页 新闻 赞助 找找看

过滤数组内非0连续区间算法求教

0
悬赏园豆:200 [已解决问题] 解决于 2014-05-14 15:04

有一个int数组,其所有数字均为非负数。

指定一个x值表示连续非0长度下限,数组中的连续非0区间的长度如果小于x,则将所有此区间内的数设为0。

再指定一个y值表示容差,数组中的非0区间统计时其中间可以出现不多于y个0值。

求此函数的较优写法。

斯克迪亚的主页 斯克迪亚 | 老鸟四级 | 园豆:4124
提问于:2014-04-30 12:50
< >
分享
最佳答案
0

同样没有看明白要求啊,需要解释一下“指定一个x值表示连续非0长度下限,数组中的连续非0区间的长度如果小于x,则将所有此区间内的数设为0。”是什么意思

收获园豆:200
于为源 | 小虾三级 |园豆:956 | 2014-05-03 08:12

就是说在遍历该数组时,遇到非0值就开始计数,直到遇到下一个0值时,判断如果计数值小于x,则将之前统计的非0值全部改为0。

斯克迪亚 | 园豆:4124 (老鸟四级) | 2014-05-04 14:19

@斯克迪亚: 代码有冗余,可以优化下,也就想到这种实现方法了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LicenceGlobal;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] tt = new int[] { 0, 1, 2, 3, 0, 0, 4, 0, 0, 0, 6, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 0, 13, 0 };

            List<int[]> r = Filter(ref tt, 6, 2);
            Console.WriteLine("处理后的数组:");
            Console.WriteLine(string.Join(",", tt));
            Console.WriteLine("------------得到的结果-------------");
            foreach (var item in r)
            {
                Console.WriteLine(string.Join(",", item));
            }
            Console.ReadLine();
        }

        static List<int[]> Filter(ref int[] array, int length, int y)
        {
            List<int[]> result = new List<int[]>();

            int start = -1;
            int end = -1;
            int count = 0;

            int i = 0;
            for (i = 0; i < array.Length; i++)
            {
                if (array[i] != 0)
                {
                    if (start == -1)
                    {
                        start = i;
                        count = 0;
                    }
                    else
                    {
                        count = 0;
                    }
                }
                else
                {
                    if (start == -1)
                    {
                        continue;
                    }
                    else
                    {
                        count++;
                        if (count > y)
                        {
                            end = i - count;
                            int tmpLenth = end - start + 1;
                            if (tmpLenth < length)
                            {
                                // 区间置为0
                                for (int j = start; j <= end; j++)
                                {
                                    array[j] = 0;
                                }
                            }
                            else
                            {
                                // 添加到结果
                                int[] t = new int[tmpLenth];
                                for (int m = 0; m < tmpLenth; m++)
                                {
                                    t[m] = array[start + m];
                                }
                                result.Add(t);
                            }
                            start = -1;
                        }
                    }
                }

                if (i == array.Length - 1)
                {
                    // 这段重复了,可以抽一个方法
                    end = i - count;
                    int tmpLenth = end - start + 1;
                    if (tmpLenth < length)
                    {
                        // 区间置为0
                        for (int j = start; j <= end; j++)
                        {
                            array[j] = 0;
                        }
                    }
                    else
                    {
                        // 添加到结果
                        int[] t = new int[tmpLenth];
                        for (int m = 0; m < tmpLenth; m++)
                        {
                            t[m] = array[start + m];
                        }
                        result.Add(t);
                    }
                }
            }            

            return result;
        }
    }
}
于为源 | 园豆:956 (小虾三级) | 2014-05-05 15:08

@于为源: 谢谢解答:-)

斯克迪亚 | 园豆:4124 (老鸟四级) | 2014-05-14 15:04

@斯克迪亚: 门外汉,估计也不是较优实现。兄弟也研究db4o?

于为源 | 园豆:956 (小虾三级) | 2014-05-14 17:12

@于为源: 呵呵,研究谈不上,只是在使用而已

斯克迪亚 | 园豆:4124 (老鸟四级) | 2014-05-14 21:07
其他回答(3)
0

你描述的我看的不明白。请列出数学公式,这样我看的直观。

wongdavid | 园豆:394 (菜鸟二级) | 2014-04-30 15:37
0

最简单的方式就是用m,n;m保存区间内第一个非0,n表示当前最后一个非0.遇到非0之后,比较n-m与x、y的大小,再考虑是否把该区间置0.

白来了123 | 园豆:160 (初学一级) | 2014-05-05 10:06
0

。。。。。

prison | 园豆:298 (菜鸟二级) | 2014-05-07 11:26
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册