相邻的数字算法石斑 [英] Adjacent number algorithm grouper

查看:131
本文介绍了相邻的数字算法石斑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是这样的:

由于数字输入设置:

1,2,3,4,5变为1-5。

1,2,3,5,7,9,10,11,12,14变为1-3,5,7,9-12,14

这是最好的,我设法想出:[C#]

这感觉有点草率给我,所以现在的问题是,是否有某种方式更具可读性和/或优雅的解决方案呢?

 公共静态字符串[] FormatInts(INT []整数)
{
    如果(整数== NULL)
        抛出新ArgumentNullException(整型); // 嘿,你在做什么?

    如果(ints.Length == 0)
        返回新的String [] {}; //没有要处理

    如果(ints.Length == 1)
        返回新的String [] {整数[0]的ToString()}; //没有要处理

    的Array.Sort< INT>(整数); //需要将这些律'婴儿整理
    名单<字符串>值=新的名单,其中,串>();

    INT lastNumber =整数[0]; //开头的第一个数字
    INT firstNumber =整数[0]; //与上述相同的

    的for(int i = 1; I< ints.Length;我++)
    {
        INT电流=整数[I]
        INT差=(lastNumber  - 电流);最后一个号码与当前数量之间//计算差异

        如果(差== -1)//数字是相邻的
        {
            如果(firstNumber == 0)//这是第一个相邻的数字
            {
                firstNumber = lastNumber;
            }
            否则//我们以某种方式在中间,或在邻近的数集的末尾
            {
                lastNumber =电流;
                继续;
            }
        }
        其他
        {
            如果(firstNumber> 0安培;&安培;!firstNumber = lastNumber)//准备打印一组数字
            {
                values​​.Add(的String.Format({0}  -  {1},firstNumber,lastNumber));
                firstNumber = 0; //复位
            }
            否则//打印单个值
            {
                values​​.Add(的String.Format({0},lastNumber));
            }
        }

        lastNumber =电流;
    }

    如果(firstNumber> 0)//如果theres什么事,把它打印出来
    {
        values​​.Add(的String.Format({0}  -  {1},firstNumber,lastNumber));
    }

    返回values​​.ToArray();
}
 

解决方案

我已经重写你的code是这样的:

 公共静态字符串[] FormatInts(INT []整数)
    {
        的Array.Sort< INT>(整数);
        名单<字符串>值=新的名单,其中,串>();

        的for(int i = 0; I< ints.Length;我++)
        {
            INT groupStart =整数[I]
            INT groupEnd = groupStart;
            而(ⅰ&所述; ints.Length  -  1和;&安培;整数[I]  - 整数[I + 1] == -1)
            {
                groupEnd =整数[I + 1];
                我++;
            }
            values​​.Add(的String.Format(groupEnd == groupStart{0}:{0}  -  {1},groupStart,groupEnd));
        }
        返回values​​.ToArray();
    }
 

然后:

  /////////////////
INT [] myInts = {1,2,3,5,7,9,10,11,12,14};
字符串[]结果= FormatInts(myInts); //现在导致富人1-3,5,7,9-12,14
 

By which I mean this:

Given the input set of numbers:

1,2,3,4,5 becomes "1-5".

1,2,3,5,7,9,10,11,12,14 becomes "1-3, 5, 7, 9-12, 14"

This is the best I managed to come up with: [C#]

Which feels a little sloppy to me, so the question is, is there somehow more readable and/or elegant solution to this?

public static string[] FormatInts(int[] ints)
{
    if (ints == null)
        throw new ArgumentNullException("ints"); // hey what are you doing?

    if (ints.Length == 0)
        return new string[] { "" }; // nothing to process

    if (ints.Length == 1)
        return new string[] { ints[0].ToString() }; // nothing to process

    Array.Sort<int>(ints); // need to sort these lil' babies
    List<string> values = new List<string>();

    int lastNumber  = ints[0]; // start with the first number
    int firstNumber = ints[0]; // same as above

    for (int i = 1; i < ints.Length; i++)
    {
        int current     = ints[i];
        int difference  = (lastNumber - current ); // compute difference between last number and current number

        if (difference == -1) // the numbers are adjacent
        {
            if (firstNumber == 0) // this is the first of the adjacent numbers
            {
                firstNumber = lastNumber;
            }
            else // we're somehow in the middle or at the end of the adjacent number set
            {
                lastNumber = current;
                continue;
            }
        }
        else
        {
            if (firstNumber > 0 && firstNumber != lastNumber) // get ready to print a set of numbers
            {
                values.Add(string.Format("{0}-{1}", firstNumber, lastNumber));
                firstNumber = 0; // reset
            }
            else // print a single value
            {
                values.Add(string.Format("{0}", lastNumber));
            }
        }

        lastNumber = current;
    }

    if (firstNumber > 0) // if theres anything left, print it out
    {
        values.Add(string.Format("{0}-{1}", firstNumber, lastNumber));                
    }

    return values.ToArray();
}

解决方案

I've rewritten your code like this:

    public static string[] FormatInts(int[] ints)
    {
        Array.Sort<int>(ints);
        List<string> values = new List<string>();

        for (int i = 0; i < ints.Length; i++)
        {
            int groupStart = ints[i];
            int groupEnd = groupStart;
            while (i < ints.Length - 1 && ints[i] - ints[i + 1] == -1)
            {
                groupEnd = ints[i + 1];
                i++;
            }
            values.Add(string.Format(groupEnd == groupStart ? "{0}":"{0} - {1}", groupStart, groupEnd));
        }
        return values.ToArray();
    }

And then:

/////////////////
int[] myInts = { 1,2,3,5,7,9,10,11,12,14 };
string[] result = FormatInts(myInts); // now result haves "1-3", "5", "7", "9-12", "14"

这篇关于相邻的数字算法石斑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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