簡體   English   中英

確定列表編號是否是連續的

[英]Determining if a list numbers are sequential

我在Java工作。 我有一個5個數字的無序列表,范圍從0到100,沒有重復。 我想檢測其中3個數字是否連續,沒有間隙。

例子:

[9,12,13,11,10] true
[17,1,2,3,5] true
[19,22,23,27,55] false

至於我試過的,還沒有。 如果我現在寫它,我可能會采用最天真的方法來排序數字,然后迭代檢查序列是否存在。

int sequenceMin(int[] set) {
    int[] arr = Arrays.copy(set);
    Arrays.sort(arr);
    for (int i = 0; i < arr.length - 3 + 1; ++i) {
        if (arr[i] == arr[i + 2] - 2) {
            return arr[i];
        }
    }
    return -1;
}

這會對數組進行排序,並使用上面的if語句查找所需的序列,並返回第一個值。


沒有排序:

(@Pace提到了非排序的願望。)有限范圍可以使用有效的“布爾數組”BitSet。 使用nextSetBit的迭代很快。

    int[] arr = {9,12,13,11,10};
    BitSet numbers = new BitSet(101);
    for (int no : arr) {
        numbers.set(no);
    }
    int sequenceCount = 0;
    int last = -10;
    for (int i = numbers.nextSetBit(0); i >= 0; i = numbers.nextSetBit(i+1)) {
        if (sequenceCount == 0 || i - last > 1) {
            sequenceCount = 1;
        } else {
            sequenceCount++;
            if (sequenceCount >= 3) {
                System.out.println("Sequence start: " + (last - 1));
                break;
            }
        }
        last = i;
    }
    System.out.println("Done");

非常天真(但速度更快)的算法:(你的數組是輸入[],假設它只包含你所說的0-100個數字)

int[] nums=new int[101];
for(i=0;i<N;i++) 
   {
   int a=input[i];
   nums[a]++;
   if (a>0) { nums[a-1]++; }
   if (a<100) { nums[a+1]++; }
   }

然后查看是否存在nums [] == 3的元素。

使用一些HashMap而不是數組可以更快(並刪除0-100限制)


編輯:唉,如果兩個數字在初始序列中相等,則不起作用

此代碼似乎正在實現您的要求:

public class OrderdedList {
    public static void main(String[] args) {
        System.out.println(orderedWithNoGap(Arrays.asList(9, 12, 13, 11, 10))); // true
        System.out.println(orderedWithNoGap(Arrays.asList(17,1,2,3,5))); // true
        System.out.println(orderedWithNoGap(Arrays.asList(19,22,23,27,55))); // false
    }

    private static boolean orderedWithNoGap(List<Integer> list) {       
        Collections.sort(list);
        Integer prev = null;
        int seq = 0;
        for(Integer i : list) {
            if(prev != null && prev+1 == i)
                seq = seq == 0 ? 2 : seq+1;
            prev = i;
        }
        return seq >= 3;
    }

}

分配大小為100的數組:

private static final int MAX_VALUE = 100;

public boolean hasSequence(int [] values) {
  int [] bigArray = new int[MAX_VALUE];

  for(int i = 0; i < values.length; i++) {
    bigArray[values[i]] = 1;
  }

  for(int i = 0; i < values.length; i++) {
    index = values[i];
    if(index == 0 || index == MAX_VALUE-1) {
      continue;
    }
    if(bigArray[index-1] == 1 && bigArray[index+1] == 1) {
      return true;
    }
  }

  return false;
}

一般算法步驟。

Step 1. Sort the array.
Step 2. There are only 3 possible groups of 3 (next to each other) in an array of length five.
indexes 0,1,2 - 1,2,3 - 2,3,4.
Step 3. Check these 3 combinations to see if the next index is 1 more than the current index.

正如OP在評論中指出的那樣,他想檢查列表是否包含3個或更多序列號

public class WarRoom {

    static final int seqCount = 3;

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>((Arrays.asList(9, 11, 123, 511, 10)));
        for (int i : list) {
            if (seqNumbers(list, i, 0) >= seqCount) {
                System.out.println("Lucky Numbers : " + (i++) + "," + (i++) + "," + i);
            }
        }
    }

    public static int seqNumbers(List<Integer> list, int number, int count) {
        if (list.contains(number)) {
            return seqNumbers(list, number + 1, count + 1);
        }
        else {
            return count;
        }
    }
}

它不是最有效的解決方案之一,但我喜歡遞歸!

沒有測試這個,但是對於小內存占用,你可以使用BitSet

private static boolean consecutive(int[] input) {
    BitSet bitSet = new BitSet(100);
    for (int num : input) {
        bitSet.set(num);
    }

    bitSet.and(bitSet.get(1, bitSet.length())); // AND shift left by 1 bit
    bitSet.and(bitSet.get(1, bitSet.length())); // AND shift left by 1 bit

    return !bitSet.isEmpty();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM