algorithm - 相邻数算法 grouper

标签 algorithm language-agnostic

我的意思是:

给定输入的数字集:

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#]

我觉得这有点草率,所以问题是,是否有更易读和/或更优雅的解决方案?

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();
}

最佳答案

我已经像这样重写了你的代码:

    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();
    }

然后:

/////////////////
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"

关于algorithm - 相邻数算法 grouper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/245735/

相关文章:

algorithm - 平衡一组数字的最佳解决方案

windows - Windows 中用户配置文件的唯一标识符

algorithm - 软件开发中的非确定性有限状态机?

python - 有效地检查字符串列表中的字符串中的单词

java - 如何为重叠 block 生成滚动校验和?

c# - IOException: 进程无法访问文件 'file path',因为它正被另一个进程使用

algorithm - 搜索算法 : Parsing and processing a request OOP style

python - 生成给定点 10 公里范围内的纬度/经度列表

c++ - trie 数据结构并打印所有子字符串

algorithm - 从二叉树中删除重复项