Discuss / Java / 使用start和end重新创建一个完成的集合,然后取两个集合的差集,有序无序都ok

使用start和end重新创建一个完成的集合,然后取两个集合的差集,有序无序都ok

Topic source

三问麻雀

#1 Created at ... [Delete] [Delete and Lock User]
static int findMissingNumber(int start, int end, List<Integer> list) {

    // 创建一个完整的集合    
    List<Integer> all = new ArrayList<>();
    for (int i = start; i <= end; i++) {
        all.add(i);
    }

    // 取两个集合之间的差集    
    all.removeAll(list);

    return all.get(0);
}

  • 1

Reply