两个有序数组长度分别为m,n,最多m+n次查找找出相同的数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
    class CompareArr
    {
        static void Main(string[] args)
        {
            try
            {
                int[] srcArr1 = new int[] { 2, 5, 6, 8, 10, 17, 29, 33, 43 };//假设数组长度为m
                int[] srcArr2 = new int[] { 1, 3, 4, 7, 8, 13, 19, 20, 29, 39, 43 };//假设数组长度为n

                int[] resultArr = GetSameItem(srcArr1, srcArr2);//方法1 面试官要的答案,所谓的最多m+n次查找
                //int[] resultArr = srcArr1.Where(t => srcArr2.Contains(t)).ToArray();//方法2:直接使用LINQ
                //int[] resultArr = GetSameItemByContains(srcArr1, srcArr2);//方法3 使用遍历加Contains方法
                //其他方法:折半查找

                if (resultArr.Length > 0)
                {
                    //Console.WriteLine(string.Format("srcArr1 {0}\nsrcArr2 {1}\n相同数字如下:", string.Join(",", srcArr1), string.Join(",", srcArr2)));
                    Console.WriteLine("srcArr1 srcArr2 相同数字如下:");
                    foreach (int item in resultArr)
                    {
                        Console.WriteLine(item.ToString());
                    }
                }
                else
                {
                    Console.WriteLine("无相同数字");
                }
                Console.Read();

            }
            catch (Exception ex)
            {
                //记录异常
            }
        }

        /// <summary>
        /// 查找两个有序数组中的相同数
        /// 面试官要的答案,所谓的最多m+n次查找
        /// </summary>
        /// <param name="srcArr1">有序数组1</param>
        /// <param name="srcArr2">有序数组2</param>
        /// <returns>数组,找到的相同数</returns>
        static protected int[] GetSameItem(int[] srcArr1, int[] srcArr2)
        {
            List<int> resultArr = new List<int>();
            int temIndex = 0;

            for (int i = 0; i < srcArr1.Length; i++)
            {
                for (int j = temIndex; j < srcArr2.Length; j++)
                {
                    if (srcArr1[i] > srcArr2[j])
                    {
                        continue;
                    }
                    else if (srcArr1[i] < srcArr2[j])
                    {
                        temIndex = j;
                        break;
                    }
                    else
                    {
                        temIndex = j + 1;
                        resultArr.Add(srcArr1[i]);
                    }
                }
            }

            return resultArr.ToArray();
        }

        /// <summary>
        /// 查找两个有序数组中的相同数
        /// 使用遍历加Contains方法
        /// </summary>
        /// <param name="srcArr1">有序数组1</param>
        /// <param name="srcArr2">有序数组2</param>
        /// <returns>数组,找到的相同数</returns>
        static protected int[] GetSameItemByContains(int[] srcArr1, int[] srcArr2)
        {
            List<int> resultArr = new List<int>();
            for (int i = 0; i < srcArr1.Length; i++)
            {
                if (srcArr2.Contains<int>(srcArr1[i]))
                {
                    resultArr.Add(srcArr1[i]);
                }
            }
            return resultArr.ToArray();
        }
    }
}

其他人的分析总结:http://blog.csdn.net/insistgogo/article/details/9228015

http://hi.baidu.com/algorithms/item/186115ec71d696abc00d757a

 

posted @ 2014-02-20 10:48  志在必得Shaun  阅读(420)  评论(0编辑  收藏  举报