向后搜索一个Array,最长的连续子 [英] Searching an Array backwards for longest contiguous subsequence

查看:73
本文介绍了向后搜索一个Array,最长的连续子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题<一href=\"http://stackoverflow.com/questions/28679991/finding-the-longest-contiguous-subsequence-in-an-array\">my previous问题。

我希望找到在向后搜索数组中的最长的连续序列。

感谢您的任何和所有帮助。

这是我目前的方式:

 公共静态无效longestBackward(INT [] ARR)
  {    INT subSeqLength = 1;
    INT最长= 1;
    布尔longestSub = FALSE;
    INT indexStart = 0;
    INT indexEnd = 0;    对(INT I = arr.length-1; ​​I&≠0;我 - )
    {//我们需要检查如果当前等于previous
        如果(改编[1] - ;常用3 [I-1])
        {//如果是我们增加的长度
            subSeqLength ++;
            //分配最长到当前子序列,如果它是最长
            如果(subSeqLength&GT;最长)
            {
                最长= subSeqLength;
                longestSub = TRUE;
            }
            //我们设置新的指数结束时,如果它是longuest            如果(longestSub)
            {
                indexStart = I + 2 - subSeqLength;
                indexEnd = I + 2;
            }        }
        //否则重新启动长度
        其他
            subSeqLength = 1;
    }    的System.out.println(最长);    //打印序列
    对于(INT I = indexEnd; I&LT; indexStart;我 - )
        System.out.print(ARR [I] +,);
  }


解决方案

我固定的code和它的评论是我变了:

 公共静态无效longestBackward(INT [] ARR){    INT subSeqLength = 1;
    INT最长= 1;
    INT indexStart = 0;
    INT indexEnd = 0;    / **
     *我们迭代而我们则是高于0而不是1,因为我们找回我 - 1
     * /
    的for(int i = arr.length - 1; I&GT; 0;我 - ){
        如果(ARR [I]&GT;常用3 [我 - 1]){//我已经改变&LT;到大于记住,我们是落后的。
            subSeqLength ++;            / *
             *我已经删除了longestSub这是没用的
            * /
            如果(subSeqLength&GT;最长){
                最长= subSeqLength;
                indexStart = I +(subSeqLength - 1); //注意,我修改了界限
                indexEnd = I - 1;
            }
        } //否则重新启动长度
        其他{
            subSeqLength = 1;
        }
    }    //打印序列
    的for(int i = indexStart - 1; I&GT; indexEnd;我 - ){
        System.out.print(ARR [I] +,);
    }
}

确认您发表评论,如果有它的某些部分,你不明白。

This is a follow-up question to my previous question.

I'm looking to find the longest contiguous sequence in an array that is searched backwards.

Thank you for any and all help.

This is my current method:

 public static void longestBackward(int[] arr)
  {

    int subSeqLength = 1;
    int longest = 1;
    boolean longestSub = false;
    int indexStart = 0;
    int indexEnd = 0;

    for (int i = arr.length-1; i > 0; i--)
    { // We need to check if the current is equal to the previous
        if (arr[i] < arr[i-1])
        {   // if it is we increment the length
            subSeqLength++;
            // Assign the longest to the current subsequence if it's the longest
            if (subSeqLength > longest)
            {
                longest = subSeqLength;
                longestSub = true;
            }
            // We set the new index end if it is the longuest

            if (longestSub)
            {
                indexStart = i + 2 - subSeqLength;
                indexEnd = i + 2;
            }

        }
        // Else re-initiate the length
        else
            subSeqLength = 1;
    }

    System.out.println(longest);

    // Print the sequence
    for (int i = indexEnd; i < indexStart; i--)
        System.out.print(arr[i] + ", ");        
  }

解决方案

I fixed your code and commented in it what I changed :

public static void longestBackward(int[] arr) {

    int subSeqLength = 1;
    int longest = 1;
    int indexStart = 0;
    int indexEnd = 0;

    /**
     * We iterate while we are higher then 0 not 1 as we retrieve i - 1
     */
    for (int i = arr.length - 1; i > 0; i--) {
        if (arr[i] > arr[i - 1]) {//I've changed < to > remember we are backward. 
            subSeqLength++;

            /*
             * I've removed the longestSub which is useless
            */
            if (subSeqLength > longest) {
                longest = subSeqLength;
                indexStart = i + (subSeqLength - 1); //notice that I modified the bounds
                indexEnd = i - 1;
            }
        } // Else re-initiate the length
        else {
            subSeqLength = 1;
        }
    }

    // Print the sequence
    for (int i = indexStart - 1; i > indexEnd; i--) {
        System.out.print(arr[i] + ", ");
    }
}

Make sure you comment if there is some part of it you don't understand.

这篇关于向后搜索一个Array,最长的连续子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆