例如有下面一个数组: 
List<string> arr = new List<string>();
            arr.Add("1");
            arr.Add("1");
            arr.Add("2");
            arr.Add("2");
            arr.Add("2");
            arr.Add("3");
            arr.Add("3");
            arr.Add("4");求一个方法,该方法 参数 是上面这个数组,方法的返回值 是:
"1" ,[0,1]
"2" ,[2,4]
"3" ,[5,6]
"4" ,[7],

解决方案 »

  1.   


    也即是说 汇总List,然后娶到 汇总前的 数组坐在的最大索引和最小索引,
    比如说 :‘1’ 在数组里索引是0和1,所以最大的索引和最小的索引是 分别是0和1,
              ‘2’ 在数组里索引是2,3,4,所以最大的索引和最小的索引是 分别是2和4,
      

  2.   


    ;WITH c1(String) AS
    (
    SELECT '1' UNION ALL
    SELECT '1' UNION ALL
    SELECT '2' UNION ALL
    SELECT '2' UNION ALL
    SELECT '2' UNION ALL
    SELECT '3' UNION ALL
    SELECT '3' UNION ALL
    SELECT '4'
    )
    ,c2 AS
    (
    SELECT 
    ROW_NUMBER() OVER(ORDER BY String) rowid,
    String
    FROM c1
    )
    SELECT String,
    Area = (SELECT '['+ cast(MIN(rowid-1) as varchar(10))+' , '+cast(MAX(rowid-1) as varchar(10))+' ]' 
    FROM c2 WHERE String = d.string) 
    FROM c2 d
    GROUP BY StringString Area
    ------ --------------------------
    1      [0 , 1 ]
    2      [2 , 4 ]
    3      [5 , 6 ]
    4      [7 , 7 ](4 行受影响)第一反应,SQL
      

  3.   

    如果你用c#来处理的话,感觉也可以把上面这段逻辑改成LINQ 或者 普通逻辑啰……
      

  4.   

                object[,] _arrary1 = (object[,])returnarray("1");
                object[,] _arrary2 = (object[,])returnarray("2");
                object[,] _arrary3 = (object[,])returnarray("3");
                object[] _arrary4 = (object[])returnarray("4");
    object returnarray(string par)
            {
                Dictionary<string, object> _arrays = new Dictionary<string, object> { 
                  {"1",new object[0,1]},
                  {"2",new object[2,4]},
                  {"3",new object[5,6]},  
                  {"4",new object[7]}            
                }; 
                return _arrays[par];
            }
      

  5.   


    List<string> arr = new List<string>();
                arr.Add("1");
                arr.Add("1");
                arr.Add("2");
                arr.Add("2");
                arr.Add("2");
                arr.Add("3");
                arr.Add("3");
                arr.Add("4");
                List<string> toarr = new List<string>();
                int f = 0;
                int t = 0;
                bool ifq = true;
                for (int i = 0; i < arr.Count; i++)
                {
                    string next = (i + 1) >= arr.Count ? "" : arr[i + 1];
                    if (arr[i] == next)
                    {
                        ifq = true;
                    }
                    else
                    {
                        t = i;
                        ifq = false;
                    }
                    if (!ifq)
                    {
                        if (f != t)
                            toarr.Add(arr[i] + ",[" + f + "," + t + "]");
                        else
                            toarr.Add(arr[i] + ",[" + t + "]");
                        f = i+1;
                    }
                    
                }
      

  6.   

    var temp = arr.Distinct();            int CurrentIndex = 0;            int lastIndex = 0;            Dictionary<string, string> dict = new Dictionary<string, string>();            for (int i = 0; i < temp.Count(); i++)
                {
                    CurrentIndex = arr.FindLastIndex(c => c == temp.ElementAt(i));
                    dict.Add(temp.ElementAt(i), lastIndex==CurrentIndex?"["+CurrentIndex+"]":"[" + lastIndex + "," + CurrentIndex + "]");
                    lastIndex = CurrentIndex+1;
                }            foreach (var item in dict.Keys)
                {
                    Console.WriteLine(item + ":" + dict[item]);
                }
      

  7.   

    public Dictionary<string, string> Get(List<string> list)
            {
                Dictionary<string, string> diction = new Dictionary<string, string>();
                int begin = 0;
                int end = 0;
                int temp = 0;
                for (int i = 0; i < list.Count-1; i++)
                {
                    begin = i;
                    for (int j = i ; j < list.Count-1; j++)
                    {
                        end = j;
                        if (j == list.Count - 2)
                        {
                            if (list[j] != list[j + 1])
                            {
                                diction.Add(list[j], "[" + begin + "-" + end + "]");
                                diction.Add(list[j + 1], "[" + (j + 1) + "-" + (j + 1) + "]");
                                i = j;
                                break;
                            }
                            else
                            {
                                diction.Add(list[j], "[" + begin + "-" + (end + 1) + "]");
                                i = j;
                                break;
                            }
                        }
                        if (list[j] != list[j + 1])
                        {
                            diction.Add(list[j], "[" + begin + "-" + end + "]");
                            i = j;
                            break;
                        }
                        
                    }
                }            return diction;
            }