private static void foo()
{
SortedList<int, List<int>> collection = new SortedList<int, List<int>>();
Random rnd = new Random();
// Filling the collection with random keys/values:
for (int i = 0; i < 100; i++)
{
int key = rnd.Next(0, 10);
if (!collection.ContainsKey(key))
collection.Add(key, new List<int>());
for (int j = 0; j < 10; j++)
{
int value = rnd.Next(0, 1000);
collection[key].Add(value);
}
}
// Displaying all pairs:
foreach (var key in collection.Keys)
{
collection[key].Sort();
for (int j = 0; j < collection[key].Count; j++)
Console.WriteLine(string.Format("[{0},{1}]", key, collection[key][j]));
}
}
var list = new List<int> { 1, 2, 2, 1, 3, 3, 3, 2, 3, 3, 1, 3, 3, 2, 3}; //example input
var results = new List<KeyValuePair<int, int>>();
int key = 0;
for (int i = 0; i < list.Count; i++)
{
if(i==0 || list[i] < list[i - 1])
key = i++; //assign key and proceed to next index (NB no index out of range checking)
results.Add(new KeyValuePair<int, int>(key, i));
}
if(i==0 || list[i] < list[i - 1])
。
编辑,将Tuple更改为KeyValuePair。试试这个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
List<int> input = new List<int>() { 1, 2, 2, 1, 3, 3, 3, 2, 3, 3, 1, 3, 3, 2, 3 };
int previous = 0;
int keyIndex = 0;
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
for (int i = 0; i < input.Count; i++)
{
if (i == 0)
{
keyIndex = 0;
}
else
{
if (input[i] < previous)
{
keyIndex = i;
}
else
{
if (dict.ContainsKey(keyIndex))
{
dict[keyIndex].Add(i);
}
else
{
dict.Add(keyIndex, new List<int>(){ i});
}
}
}
previous = input[i];
}
foreach (int dictKey in dict.Keys)
{
var l = dict[dictKey];
Console.WriteLine("Key:{0}, values={1}", dictKey, string.Join(",", dict[dictKey].Select(x => x.ToString()).ToArray()));
}
Console.ReadLine();
}
}
}
我希望它能解决你的问题
public void GetPairs()
{
List<int> list = new List<int>() { 1,3,3,2,3,3,1,3,4,1 };
List<KeyValuePair<int, int>> kvapar = new List<KeyValuePair<int, int>>();
int prev = 0, firstIndex = 0, lastIndex = 0;
foreach (int i in list)
{
if (i < prev)
{
kvapar.AddRange(GetRange(list.GetRange(firstIndex, lastIndex)));
firstIndex += lastIndex; lastIndex = 0;
}
prev = i; lastIndex++;
}
}
public List<KeyValuePair<int, int>> GetRange(List<int> list)
{
List<KeyValuePair<int, int>> result = new List<KeyValuePair<int, int>>();
foreach(int i in list)
{
if (list.IndexOf(i) == 0) continue;
result.Add(new KeyValuePair<int, int>(list[0], i));
}
return result;
}
SortedList<int,List<int>>
。当你需要输出时,只需遍历所有键(不要忘记在开始迭代之前使用List.Sort
方法进行排序)。如果你想要的话,我可以轻松地分享一段代码片段。 - Nissim