已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题吗?**通过editing this post添加详细信息并阐明问题。
4天前关闭。
Improve this question
bool secuence=true;
List<int> list= new List<int> { 1, 2, 3, 4, 6, 8, 9, 10 };
for (int i = 1; i < list.Count; i++)
{
//check if current value is +1 greater than last value
if (list[i] + 1 == list[i - 1] || list[i] - 1 == list[i - 1] && secuence == true)
{ //continue counting if consecutive
secuence = true;
if (secuence)
{
Debug.Log(i + " seqqq");
}
}
else
{ //Stop Counting if not consecutive
secuence = false;
}
}
----解决了
var list = new List<int> { 1, 2, 3, 6, 4, 8, 9, 10 ,14};
list.Sort();
var findlist = new List<int> { };
for (int i = 0; i <= list.Count-1; i++)
{
if (i + 1 < list.Count)
{
if (list[i] + 1 == list[i + 1])
{
//Debug.Log(list[i] + " seqqq");
findlist.Add(list[i]);
}
else if ((list[i] + 1 != list[i + 1]) && (list[i] - 1
== list[i - 1]))
{
//Debug.Log(list[i] + " seqqq");
findlist.Add(list[i]);
}
}
else
{
if (list[i] - 1 == list[i - 1])
{
//Debug.Log(list[i] + " seqqq");
findlist.Add(list[i]);
}
}
}
var diffs =
list.Union(findlist).Except(list.Intersect(findlist));
foreach (var item in diffs)
{
Debug.Log(item);
}
找到连续编号后,将哪个编号删除主列表,如新建列表={6};例如,主列表是{ 1,6,3,4,6,5,9,11 };连续数是3 4 5 6,新列表是{1,6,9,11};
我通过使用2个单独的列表解决了这个问题。我可以单独使用连续的,我可以删除那些不在列表中的。
1条答案
按热度按时间e37o9pze1#
如果要了解序列中的所有数字是否都递增:
或者用一种速记方式: