Nat*_*ate 25 .net c# linq sorting .net-3.5
我是否有可能以一种允许我确定"9"是排序列表中第一个缺失值而不使用for循环并将每个值与其相邻的值进行比较的方式使用LINQ?
var listStringVals = new [] { "7", "13", "8", "12", "10", "11", "14" };
// sort list to "7","8","10","11","12","13","14"
var sortedList = listStringVals.OrderBy(c => int.Parse(c)).ToList();
// need some magic here to get the first gap in the sorted list
Run Code Online (Sandbox Code Playgroud)
aba*_*hev 61
让
var strings = new string[] { "7", "13", "8", "12", "10", "11", "14" };
Run Code Online (Sandbox Code Playgroud)
然后
var list = Array.ConvertAll(strings, s => Int32.Parse(s)).OrderBy(i => i);
// or
var list = strings.Select(s => int.Parse(s)).OrderBy(i => i);
// or
var list = strings.OrderBy(s => int.Parse(s));
Run Code Online (Sandbox Code Playgroud)
(注意这个问题)
然后
var result = Enumerable.Range(list.Min(), list.Count).Except(list).First(); // 9
// or
int min = list.Min(), max = list.Max();
var result = Enumerable.Range(min, max - min + 1).Except(list).First();
Run Code Online (Sandbox Code Playgroud)
Bee*_*Guy 12
这是一个让你入门的方法(我int
在这里使用了值):
List<int> listStringVals = (new int[] { 7, 13, 8, 12, 10, 11, 14 }).ToList();
List<int> SortedList = listStringVals.OrderBy(c => c).ToList();
List<int> Gaps = Enumerable.Range(SortedList.First(),
SortedList.Last() - SortedList.First() + 1)
.Except(SortedList).ToList();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10553 次 |
最近记录: |