数组列表并找到具有相同编号的最长子序列 [英] Array list and finding the longest subsequence with the same number

查看:218
本文介绍了数组列表并找到具有相同编号的最长子序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是实现此目标的最佳方法.

I was wondering what would be the best way to implement this.

想不出一种好方法来保存需要保存的信息,例如 索引和值的数量,最后是重复的实际数量

Can't think of a good way to save what information that needs to be saved like the index and the number of values and finally the actual number that get's repeated

public class testing 
{

public static void main(String[] args) 
{
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    Scanner in = new Scanner(System.in);
    Integer a =0;
    Integer value = 0;
    Integer num = 0;

    boolean loop = true;
    //getting the string information
    while(loop)

    {
        System.out.println("Enter a series of numbers, 0 to stop");
        Integer n = in.nextInt();
        if(n.equals(0))
        {
            break;
        }
        else
        { 
            numbers.add(n);         

        }



    }

    for (int i = 1; i < numbers.size(); i++)
    { 




    }

}



}

推荐答案

您可以使用2D ArrayList,声明如下:

You could use a 2d ArrayList, declared like this:

ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

,然后在过程结束时声明要添加到其中的2个ArrayList:

and then declare the 2 ArrayLists to be added to it at the end of the process:

ArrayList<Integer> length = new ArrayList<Integer>();

ArrayList<Integer> value = new ArrayList<Integer>();

然后

1)遍历列表,检查元素是否与先前相同.

1) iterate through the list checking if the element is the same as previous.

如果是,请继续进行到最后,或者发现一个不同的元素,此时在ArrayList中将先前相等元素的数量存储在"length"中,并将该元素的值存储在一个"value"中.有一个int(称为索引称),它存储元素的索引,该索引的长度包含当前最长子序列的长度(它将与包含其组成的元素的值的元素的索引相同(已存储在值中)).

If it is, carry on until end or an element is discovered which differs, at which point store the number of the previous equal elements in the ArrayList called 'length' and the value of the element in the one called 'value'. Have an int (called index say) which stores the index of the element in length containing the length of the longest current subsequence (which will be the same as the index of the element containing the value of the element it's made up of (which has been stored in value)).

如果不是,请移至下一个元素.

If it's not, move to the next element.

2)重复此过程,如有必要(即,如果发现更长的子序列),则更新索引.

2) Repeat the process, updating index if necessary (i.e. if a longer subsequence is discovered).

要在末尾添加长度和值,只需执行result.add(length);result.add(value);

To add length and value to result at the end, just do result.add(length); and result.add(value);

如果要返回一个包含所有必需信息的对象,则可以将int'index'包装在Integer中,并将其添加到名为'length'的ArrayList的末尾,甚至可以将其放入新的ArrayList中,将ArrayList添加到结果中.

If you want to return one object that holds all the required info, you could wrap the int 'index' in an Integer and add it to the end of the ArrayList called 'length' or even put it in a new ArrayList and add that ArrayList to result.

请注意,要在存储到结果中的第一个ArrayList中的索引i处检索元素(在本例中为"length"),您需要进行

Note that to retrieve an element at index i in the first ArrayList (in this case the one called 'length') after it has been stored in result, you would need to do

result.get(0).get(i);

所以我想到的for循环部分是这样的:

So the for loop part I had in mind would be this:

boolean same = false;
int sequenceLength = 0;
Integer sequenceInteger = null; 

for (int i = 1; i < numbers.size(); i++)
        { 
            if(numbers.get(i).equals(numbers.get(i-1)))
                {
                      same = true;
                      sequenceLength++;
                }      
            else(if same == true)
                {
                      sequenceInteger = new Integer(sequenceLength);
                      //add sequenceInteger to length and numbers.get(i-1) to value 
                      same = false;
                      sequenceLength = 0;
                }
            // else do nothing since same is false, which means that the current
            // element is different from the previous and the previous is 
            // different the one before that, so there are no new values to store
        }
// end of list reached
(if same == true)
{
      sequenceInteger = new Integer(sequenceLength);
      //add sequenceInteger to length and numbers.get(i-1) to value 
      same = false;
}

这篇关于数组列表并找到具有相同编号的最长子序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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