最简单方便的算法 如何写?

解决方案 »

  1.   

    private bool ThreeNumbers(int[] array)
    {
    ArrayList list=new ArrayList();
    foreach(int n in array)
    {
    if(!list.Contains(n))
    {
    list.Add(n);
    if(list.Count>3) return false;
    }
    }
    if(list.Count==3) return true;
    else return false;
    }
      

  2.   

    用一个局部变量来计数,应该会更快。 private bool ThreeNumbers(int[] array)
    {
    ArrayList list=new ArrayList();
    int count=0;
    foreach(int n in array)
    {
    if(!list.Contains(n))
    {
    list.Add(n);
    count++;
    if(count>3) return false;
    }
    }
    if(count==3) return true;
    else return false;
    }
      

  3.   

                if(count==3) return true;
                else return false;
    为什么不写成
    return count == 3;
      

  4.   

    按我理解输入输出是不是这样的?
    1,1,1,2,2,3 = false
    1,1,2,2,3,3 = false
    1,2,3,4,4,4 = true
    1,2,2,2,3,4 = true
      

  5.   

      private bool ThreeNumbers(int[] array)
            {
                ArrayList list=new ArrayList();
                foreach(int n in array)
                {
                    if(!list.Contains(n))
                    {
                        list.Add(n);
                        if(list.Count>3) return false;
                    }
                }
                if(list.Count==3) return true;
                else return false;
            }
      

  6.   


    int[] tempArrayInt = new tempArrayInt [6];//存放你的数组
    List<Int> tempList = new List<Int>();
    for(int i= 0;i <tempArrayInt .Length;i++ )
    {
       if(tempList.Count > 0)
    {
       for(int j=0;j<tempList.Count;j++)
    {
    if(tempArrayInt [i] == tempList[j])
    {
    break;
    }
    else
    {
    if(j == tempList.Count -1 && tempArrayInt [i] != tempList[j])
    {
    tempList.Add(tempArrayInt [i]);
    }
    }
    }
    }
    else
    {
       tempList.Add(tempArrayInt [i]);
    }if(tempList.Count != 3)
    {
     return false;
    }
    else
    {
    return true;
    }
    }
      

  7.   

    int []a=new int[6]
    for(int i=1;i<a.length;i++)
    {
    if(a[i]!=0])
    {
    int s=a[i]/a[i-1]{
    if(s==1)int aa=s++;
    if(aa==3)
    return;}
    }}
      

  8.   

    Linq最简洁...retutn (array.Distinct().Count() - 1) == 3;
      

  9.   

    private void  Form1_Load(object sender, EventArgs e)
            {
                //先排序,然后相临的相减. 仅有相临的3个相减结果等于零的时候return true . 其他的false ;
                int[] a =new int[]{ 1,1,5,1,9,7};
                Array.Sort(a);
                int[] result = new int[5];
                result[0] = a[0]-a[1]; 
                result[1] = a[1]-a[2];
                result[2] = a[2]-a[3];
                result[3] = a[3]-a[4];
                result[4] = a[4]-a[5];
                int j =0 ;
                for (int i=0 ; i<5;i++)
                { 
                    if (result[i]==0)
                    j++;
                }
                if (j == 2)
                {
                    MessageBox.Show("true");
                }
                else
                {
                    MessageBox.Show("false");            }        }
    逻辑上好象简单些.虽涉及数组排序,但时间复杂度上还是我想到最小的,其他的要涉及好多比较过程,其实就是变相的排序.还会比先排序耗时多些!
      

  10.   

    retutn (array.Distinct().Count() - 1) == 3;呵呵,我看到问题就直接想Distinct了,还没回就看见11楼的了