从2D Arraylist

时间:2015-09-18 00:17:01

标签: java arraylist

方法public List horizo​​ntalSequences()返回水平行中相同元素的最长序列的列表。例如,如果我有

ArrayList<ArrayList<String>> list = [. . . c c . 

                                     a a a a . . 

                                     b b b b b b 

                                     z z . c c c 
                                                ]

然后该方法应返回包含所有6 b s([b,b,b,b,b,b])的列表。我真的陷入了这个问题,如果smb可以看看代码,我会很感激。我不认为我的代码逻辑是正确的,它根本不能正常工作。但如果我有这个清单怎么办:  的ArrayList&GT; list =

                                    [. . .  c . 

                                     . . . a . . 

                                     . . . . . . 

                                     . . . c . . 
                                                ]

然后该方法应仅返回第一次出现,即列表中的第一个元素[c]。  提前谢谢!

public List<String> gorizontalSequence(ArrayList<ArrayList<String>> list){
     ArrayList<String> list = new ArrayList<String>();
     int currentCount = 0;
     int max = 0;

     for(int i = 0; i < list.size(); i++){
         for(int j = 0; j < list.get(i).size() - 2;){

             if(list.get(i).get(j).equals(list.get(i).get(j-1)) 
                     && (!(list.get(i).get(j).equals("."))) 
                             && (!(list.get(i).get(j-1).equals(".")))){


                 currentCount+=2;
                 if(currentCount > max){
                     max = currentCount;
                 }

                 //list.clear();
                 list.add(list.get(i).get(j-1));
                 list.add(list.get(i).get(j));
                 j+=2;

             }

             else{
             list.add(list.get(i).get(j-1));
             currentCount++;
             if(currentCount > max){
                 max = currentCount;
            } 
                 j++;
         }
     }

4 个答案:

答案 0 :(得分:0)

所以你要说的是 - 你想找到一个字母的最大序列。我不会为你编写整个程序,但有一种方法可以在一次迭代中完成:

方法

我们将在遍历列表时将当前字母的值保存在临时变量中。然后我们将看看下一个是否也是临时变量。如果是这种情况,我们会增加一个包含连续字母数的临时变量。如果不是这种情况,我们检查该临时变量是否大于最大序列(存储在另一个变量中)。如果是,我们覆盖max并将temp var设置为0,否则,我们将其设置为0并重新开始。

伪代码

//I will assume it's an array for now
char currentChar = list[0]
int maxSeq, maxIndex
int curSeq 
for (int i = 0; i < list.length; i++)
    char = list[i]
    if currentChar == c
        curSeq++
    else
        if curSeq > maxSeq //ONLY if it's greater. If it's equal to then we will get the LAST sequence max
            maxSeq = curSeq
            maxIndex = i
            curSeq = 0
        else 
            curSeq = 0

//now you have the index at which it starts and the length of the sequence

希望你明白这一点。

答案 1 :(得分:0)

这应该做:

Drag the code itself (.swift files) to your project. As sadly, Swift currently does not support compiling Frameworks for iOS 7.
Make sure that the files are added to the Target membership.

答案 2 :(得分:0)

如果我理解问题是正确的,那么'给出一个列表列表,找到任何一个列表中最长的连续序列的相等元素'。也就是说连续序列不能跨列表分割。

如果是这种情况,那么你可以分两步完成:

  1. 对于每个列表,缩小到该列表中最长的序列。
  2. 查找最长的列表。
  3. 所以第一个问题是一个元素列表,这个列表中最长的连续序列是什么。

    private static <E> List<E> longestConsecutiveSequence(List<E> list) {
        List<E> longestSequence = new ArrayList<>();
        List<E> currentSequence = new ArrayList<>();
        for (E el: list) {
            if (!currentSequence.isEmpty() && !e.equals(currentSequence.get(0)))
                currentSequence = new ArrayList<>();
            currentSequence.add(e);
            if (currentSequence.size() > longestSequence.size())
                longestSequence = currentSequence;
        }
        return longestSequence;
    }
    

    第二个问题是找到最长的清单:

    public static <E> List<E> longestConsecutiveSequence(List<List<E>> listOfLists) {
        List<E> longestSequence = null;
        for (List<E> list: listOfLists) {
            List<E> sequence = longestConsecutiveSequence(list);
            if (longestSequence == null || sequence.size() > longestSequence.size())
                longestSequence = sequence;
        }
        return longestSequence;
    }
    

    请注意,如果您使用的是Java 8,那么事情会变得更加简单。例如,第二种方法变为:

    return listOfList.stream()
        .map(this::longestConsecutiveSequence)
        .max(Comparator.comparingInt(List::size));
    

    第一个也可以用流完成,但它有点棘手,因为Java流旨在独立处理项目,因此检查序列非常重要。如果您希望我发布或者过于详细,请告诉我。

答案 3 :(得分:0)

创建一个与列表大小相同的int数组,以保存元素的“点”。 如果当前元素等于前一个元素,则其点是前一个元素点加1。 获取最大点加1,我们有最大范围

    List<String> list = Arrays.asList("v", "a", "a", "a", "a", "a", "a", "b", "a", "a", "a", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "c", "a", "a", "a");
    int[] points = new int[list.size()];
    int maxPos = 0;

    for (int i = 1; i < list.size(); i++) {
        if (list.get(i).equals(list.get(i - 1)))
            points[i] = points[i - 1] + 1;
        if (points[i] > points[maxPos])
            maxPos = i;
    }

    System.out.println(Arrays.toString(points));
    System.out.println("Max range size: " + (points[maxPos] + 1));
    System.out.println("With element: " + list.get(maxPos));

现在您可以创建一个大小为 points[maxPos] + 1 的ArrayList,并添加 list.get(maxPos)

的元素