Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#未排序的最长非递减序列的长度_C#_Arrays_List_Indexing - Fatal编程技术网

C#未排序的最长非递减序列的长度

C#未排序的最长非递减序列的长度,c#,arrays,list,indexing,C#,Arrays,List,Indexing,我有一个列表,其中包含一些值 for (int i = 0; i < InputNumbers.Length; i++) { InputNumbers[i] = Convert.ToInt32(Console.ReadLine()); LongestSequence.Add(InputNumbers[i]); } for (int i = 0; i < InputNumbers.Lengt

我有一个列表,其中包含一些值

for (int i = 0; i < InputNumbers.Length; i++)
        {
            InputNumbers[i] = Convert.ToInt32(Console.ReadLine());
            LongestSequence.Add(InputNumbers[i]);
        }
        for (int i = 0; i < InputNumbers.Length; i++)
        {
            if (i > 0)
            {
                if (LongestSequence[i] < LongestSequence[i - 1])
                {
                    LongestSequence.Remove(LongestSequence[i]);
                    LongestSequence.Insert(i, null);
                }
            }
        }
我想知道这两个空值之间的值的长度。在本例中是2


  • 如果输入为1 2 3 0,则null将在开始处,并将替换零,因为0小于3,并且我正在寻找最长的非递减序列

看起来像是XY问题。你需要找到最长的非递减序列,你已经找到了一个可怕的方法,现在你想要我们帮助你。最好直接请求帮助处理最长的非递减序列。不需要在设置
null
s中然后尝试在它们之间查找值

您可以简单地执行以下操作:

List<int> current = new List<int> { Int32.MaxValue };
int[] best = new int[0];

int n = int.Parse(Console.ReadLine());   

for (int i = 0; i < n; i++)
{
    var read = int.Parse(Console.ReadLine());

    if (read < current[current.Count - 1])
    {
        if (current.Count > best.Length) best = current.ToArray();
        current.Clear();
    }

    current.Add(read);
}

if (current.Count > best.Length) best = current.ToArray();

// Here, "best" stores the best result
List current=新列表{Int32.MaxValue};
int[]最佳=新int[0];
int n=int.Parse(Console.ReadLine());
对于(int i=0;ibest.Length)best=current.ToArray();
current.Clear();
}
当前。添加(读取);
}
如果(current.Count>best.Length)best=current.ToArray();
//在这里,“最佳”存储最佳结果
它看起来更加清晰、透明和可读。

然而,我坚信还有很多更简短、更优雅的解决方案。

列表中只有两个空值吗?请解释这段代码的目的是什么,因为它看起来非常不可压缩不可能有一百个空值。当列表中有几百个空值时,输出应该是什么?如果输入是1 2 3 0null将在开始处,并将替换零,因为0小于3,我正在寻找最长的非递减值sequence@kopelence如果对我有用的话。尝试更新的代码。请注意,
n
首先被读取。
List<int> current = new List<int> { Int32.MaxValue };
int[] best = new int[0];

int n = int.Parse(Console.ReadLine());   

for (int i = 0; i < n; i++)
{
    var read = int.Parse(Console.ReadLine());

    if (read < current[current.Count - 1])
    {
        if (current.Count > best.Length) best = current.ToArray();
        current.Clear();
    }

    current.Add(read);
}

if (current.Count > best.Length) best = current.ToArray();

// Here, "best" stores the best result