代码之家  ›  专栏  ›  技术社区  ›  Manish Basantani

将两个列表合并为一个列表并对项目进行排序

  •  4
  • Manish Basantani  · 技术社区  · 16 年前

    有没有一种方法可以将两个给定的列表合并到一个列表中,并使用一个for循环以排序的方式存储这些项?

    另外,我正在寻找一种不使用API方法的解决方案(如联合、排序等)。

    示例代码。

    private static void MergeAndOrder() 
    {
    var listOne = new List<int> {3, 4, 1, 2, 7, 6, 9, 11}; 
    var listTwo = new List<int> {1, 7, 8, 3, 5, 10, 15, 12}; 
    
    //Without Using C# helper methods...
    //ToDo.............................
    
    //Using C# APi.
    var expectedResult = listOne.Union(listTwo).ToList(); 
    expectedResult.Sort();//Output: 1,2,3,4,5,6,7,8,9,10,11,12,15
    //I need the same result without using API methods, and that too by iterating over items only once.
    
    
    }
    

    附言:我在一次采访中被问到这个问题,但至今还没有找到答案。

    8 回复  |  直到 8 年前
        1
  •  8
  •   Jonathon Faust    16 年前

    List a = { 1, 3, 5, 7, 9 }
    List b = { 2, 4, 6, 8, 10 }
    List result = { }
    int i=0, j=0, lastIndex=0
    while(i < a.length || j < b.length)
        // If we're done with a, just gobble up b (but don't add duplicates)
        if(i >= a.length)
            if(result[lastIndex] != b[j])
                result[++lastIndex] = b[j]
            j++
            continue
    
        // If we're done with b, just gobble up a (but don't add duplicates)
        if(j >= b.length)
            if(result[lastIndex] != a[i])
                result[++lastIndex] = a[i]
            i++
            continue
    
        int smallestVal
    
        // Choose the smaller of a or b
        if(a[i] < b[j])
            smallestVal = a[i++]
        else
            smallestVal = b[j++]
    
        // Don't insert duplicates
        if(result[lastIndex] != smallestVal)
            result[++lastIndex] = smallestVal
    end while
    
        2
  •  6
  •   Joel Coehoorn    16 年前

    .ToList() .ToArray()

    var expectedResult = listOne.Union(listTwo).OrderBy(i => i);
    

        3
  •  1
  •   Sailaja V    8 年前
        private static void MergeTwoSortedArray(int[] first, int[] second)
        {
            //throw new NotImplementedException();
    
            int[] result = new int[first.Length + second.Length];
    
            int i=0 , j=0 , k=0;
    
            while(i < first.Length && j <second.Length)
            {
                if(first[i] < second[j])
                {
                    result[k++] = first[i++];
                }
                else
                {
                    result[k++] = second[j++];
                }
            }
    
            if (i < first.Length)
            {
                for (int a = i; a < first.Length; a++)
                    result[k] = first[a];
            }
    
            if (j < second.Length)
            {
                for (int a = j; a < second.Length; a++)
                    result[k++] = second[a];
            }
    
            foreach (int a in result)
                Console.Write(a + " ");
    
            Console.WriteLine();
        }
    
        4
  •  1
  •   Bartek    8 年前

            var list1 = new List<int?>() {
                1,3,5,9,11
            };
    
            var list2 = new List<int?>() {
                2,5,6,11,15,17,19,29
            };
    
            foreach (var c in SortedAndMerged(list1.GetEnumerator(), list2.GetEnumerator())) {
                Console.Write(c+" ");
            }
    
            Console.ReadKey();
        }
    
        private static IEnumerable<int> SortedAndMerged(IEnumerator<int?> e1, IEnumerator<int?> e2) {
            e2.MoveNext();
            e1.MoveNext();
    
            do {
                while (e1.Current < e2.Current) {
                    if (e1.Current != null) yield return e1.Current.Value;
                    e1.MoveNext();
                }
    
                if (e2.Current != null) yield return e2.Current.Value;
                e2.MoveNext();
            } while (!(e1.Current == null && e2.Current == null));
        }
    }
    
        5
  •  0
  •   LBushkin    16 年前

        6
  •  0
  •   Chris R. Timmons    16 年前
    var listOne = new List<int> { 3, 4, 1, 2, 7, 6, 9, 11 };
    var listTwo = new List<int> { 1, 7, 8, 3, 5, 10, 15, 12 };
    var result = listOne.ToList();
    
    foreach (var n in listTwo)
    {
      if (result.IndexOf(n) == -1)
        result.Add(n);
    }
    
        7
  •  0
  •   ewernli    16 年前

    int[] values = new int[ Integer.MAX ]; // initialize with 0
    int size1 = list1.size();
    int size2 = list2.size();
    
    for( int pos = 0; pos < size1 + size2 ; pos++ )
    {
         int val =  pos > size1 ? list2[ pos-size1 ] : list1[ pos ] ;
         values[ val ]++;
    }
    

    values 0

        8
  •  0
  •   paulH    12 年前

    List<int> sortedList = new List<int>();
    foreach (int x in listOne)
    {
        sortedList<x> = x;
    }
    foreach (int x in listTwo)
    {
        sortedList<x> = x;
    }