zoukankan      html  css  js  c++  java
  • 合并两个有序数组

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication8
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] a = { 11, 12, 66, 99 };
                int[] b = { 7, 8, 13, 44 };
    
                List<int> alist = new List<int>();
                List<int> blist = new List<int>();
                for (int i = 1; i <= 100 * 10000; i++)
                {
                    alist.Add(i);
                }
                for (int i = 1000001; i <= 200 * 10000; i++)
                {
                    blist.Add(i);
                }
                //a = alist.ToArray();
                //b = blist.ToArray();
                int[] c = MergeArr(a, b);
                for (int i = 0; i < c.Length; i++)
                {
                    Console.WriteLine(c[i]);
    
                }
                Console.Read();
            }
    
            private static T[] MergeArr<T>(T[] a, T[] b) where T : IComparable
            {
                List<T> arr = new List<T>();
                int maxIndex = a.Length - 1;
                bool ia = false;
                bool ib = false;
                int aindex = 0;
                int bindex = 0;
                while (true)
                {
    
                    if (aindex > maxIndex)
                    {
                        ia = true;
                        break;
                    }
                    if (bindex > maxIndex)
                    {
                        ib = true;
                        break;
                    }
    
                    if (a[aindex].CompareTo(b[bindex]) <= 0)
                    {
                        arr.Add(a[aindex]);
                        aindex++;
                    }
                    else
                    {
                        arr.Add(b[bindex]);
                        bindex++;
    
                    }
                }
    
                if (ia)
                {
                    for (int index = bindex; index <= maxIndex; index++)
                    {
                        arr.Add(b[index]);
                    }
                }
                if (ib)
                {
                    for (int index = aindex; index <= maxIndex; index++)
                    {
                        arr.Add(a[index]);
                    }
                }
                return arr.ToArray();
            }
        }
    }
  • 相关阅读:
    [NOI2012]美食节——费用流(带权二分图匹配)+动态加边
    mysqlsla slow-query常用用法
    [POI2000]病毒
    mysqlsla安装
    mysqldumpslow
    [学习笔记]约数
    查看MySQL数据的连接
    [学习笔记]质数
    关于ulimit -a中需要修改的两个值
    Miller-Rabin与二次探测
  • 原文地址:https://www.cnblogs.com/kexb/p/9261245.html
Copyright © 2011-2022 走看看