有一整数数组 例如:(10,50,3,4,),请你找出该数组中连续增加的数中最长的个数

解决方案 »

  1.   

    private int Foo(int[] arr, int startIndex, int endIndex)
    {
    int totalMaxCount = 1;
    int currentMaxCount = 1;
    int t = arr[0];
    for (int i = startIndex + 1; i < endIndex; i++)
    {
    if (arr[i] > t)
    {
    currentMaxCount++;
    if (currentMaxCount > totalMaxCount)
    {
    totalMaxCount = currentMaxCount;
    }
    }
    else if (arr[i] < t)
    {
    currentMaxCount = 1;

    }
    t = arr[i];
    }
    return totalMaxCount;
    }int a = Foo(arr, 0, arr.Length);
      

  2.   

    public void FetMaxLength()
            {
                int[] intArray = { 1, 2, 2, 4, 5, 10, 7, 8, 9, 50, 1, 2, 2, 5, 2, 7, 2, 2, 10, 11, 20 };
                int intLength = 1;//当前长度
                int j = 1;//以前最大长度
                for (int i = 0; i < intArray.Length; i++)
                {
                    if (i != 0)
                    {
                        //熟悉连续,长度+1
                        if (intArray[i] - intArray[i - 1] == 1)
                        {
                            intLength++;
                        }
                        //数字不连续,计算出前面连续数字的长度
                        else
                        {
                            //如果此次连续数字长度大于以前最大长度
                            if (intLength >= j)
                            {
                                //最大长度为
                                j = intLength;
                            }
                            //当前数字长恢复为1
                            intLength = 1;
                        }
                    }
                }
                int maxLength = j;
            }
      

  3.   

    说下我的思路,两两求差,大于0的设成a,其余为b,组成一个string。
    比如2 3 4 3 2 3就是aabba
    然后string.split(b)
    求出分离后的最长的string长度即可
      

  4.   

        class Program
        {
            static void Main(string[] args)
            {
                int[] arr = new int[] { 5, 4, 3, 2, 3 };            Function(arr);            Console.ReadKey();
            }        public static void Function(int[] arr)
            {
                int startIndex = 0, tmpStartIndex = 0;
                int maxLength = 0, tmpMaxLength = 0;
                bool isGrowing = false;            for (int i = 0; i < arr.Length - 1; i++)
                {
                    if (arr[i] - arr[i + 1] < 0)
                    {
                        // now growing
                        if (isGrowing == false) // first enter growing queue
                        {
                            isGrowing = true;
                            tmpStartIndex = i;
                            tmpMaxLength = 2;
                        }
                        else // already in a growing queue
                        {
                            tmpMaxLength++;
                        }
                    }
                    else
                    {
                        // growing queue end
                        isGrowing = false;
                    }                // save the current growing queue info if it is longer than before
                    if (tmpMaxLength > maxLength)
                    {
                        maxLength = tmpMaxLength;
                        startIndex = tmpStartIndex;
                    }
                }            // 输出最大增长子数组的起始位置和长度
                Console.WriteLine("StartIndex: {0}, Length: {1}", startIndex, maxLength);
            }
        }