KeyValuePair组列表值

时间:2015-08-24 11:46:44

标签: c# algorithm list

说我有一个清单

[1,2,2,1,3,3,3,2,3,3,1,3,3,2,3]

enter image description here

任何想法如何对它们进行分组(List<KeyValuePair<int, int>>)以使密钥是下一个最小的数字,并且值是下一个最大的数字,如果它重复自身,则使用相同的最小密钥对其进行分组,如果这样做的话感...

这是我要找的输出:

[Key, Value]
[0,1]
[0,2]
[3,4]
[3,5]
[3,6]
[7,8]
[7,9]
[10,11]
[10,12]
[13,14]

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

基于图像和示例输入:

       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替换为:if(i==0 || list[i] < list[i - 1])

编辑,使Tuple成为KeyValuePair

答案 2 :(得分:1)

试试这个

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

        }
    }
}
​