一个无序数组里有 99 个不重复正整数,范围从1到100,唯独缺少一个整数,如何找出这个缺失的整数?

感觉本次内容没什么好讲的,直接上代码,程序中实现了三种方式,如果有更多方式,请各位小伙伴留言:

注意:代码使用JDK为1.8版本,里面用到了新特性

public class Test1 {
    /**
     *  题目:
     *  一个无序数组里有 99 个不重复正整数,范围从1到100,唯独缺少一个整数,如何找出这个缺失的整数?
     */


    public static void main(String[] args) {

        try {
            List<Integer> list = createRandomNumber();
            System.out.println("生成的数组的值:");
            list.stream().forEach(System.out::println);

            findMethod1(list); //调用方法1

            findMethod2(list); //调用方法2

            findMethod3(list); //调用方法3

        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static List<Integer> createRandomNumber(){
        List<Integer> list = new ArrayList<>();
        try {
            /***
             * 用随机方式循环产生100个不重复的整数,当数组要满的时候,会消费很长时间才会生成最后一个值,因为随机数生成无法控制
             * 为了演示方便,此处按顺序生成1-100的数
             */
            int max = 100, min = 1;
            int ran2 = (int) (Math.random() * (max - min) + min);
            System.out.println("生成的1-100的随机数:"+ran2);
            for (int i=1;i<=100;i++){
                list.add(i);
            }
            /**
             *  调用 remove 方法的时候需要注意,如果传入基本类型 默认是根据 index 索引去删除集合中的数据
             *  需要传入对象也就是引用类型才会根据内容去删除数据,注意
             *  int = 0 传入只会删除集合中第一个索引位置的元素,就算其中的内容不是0也会删除,此时根据索引来删除
             *  Integer = 0 此时根据内容来删除,因为下标是基本类型int,现在传入一个对象,就会删除集中所有内容为 0 的数据
             */
            list.remove((Integer) ran2);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return list;
    }


    /**
     * 算法1
     * 可以无视值在集合中的顺序
     */
    public static void findMethod1(List<Integer> list) throws Exception{
        Map<String,String> temp = new HashMap<>();
        for (int i=1;i<=100;i++){
            temp.put(i+"",i+"");
        }
        list.stream().forEach((key)->{
            temp.remove(key.toString());
        });
        temp.forEach((a,b)->{
            System.out.println("method1缺少的整数:"+b);
        });
    }

    public static void findMethod2(List<Integer> list) throws Exception{
        for (int i=0;i<list.size();i++){
            if ( i == list.size() - 1){
                if(list.contains(1)){
                    System.out.println("method2缺少的整数:100");
                }else if (list.contains(100)){
                    System.out.println("method2缺少的整数:1");
                }
                break;
            }
            int preInt = list.get(i);
            int nextInt = list.get(i+1);
            if ((nextInt - preInt) > 1){
                System.out.println("method2缺少的整数:"+(list.get(i)+1));
                break;
            }
        }
    }

    public static void findMethod3(List<Integer> list)throws Exception{
        int sum1=0;
        for (int i=1;i<=100;i++){
            sum1+=i;
        }

        Integer listSum =  list.stream().reduce(0,(x, y) -> x + y);

        System.out.println("method3缺少的整数:"+(sum1 - listSum));
    }

}

执行结果,三个方法的值相同:
生成的1-100的随机数:18
生成的数组的值:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
method1缺少的整数:18
method2缺少的整数:18
method3缺少的整数:18

Process finished with exit code 0
 

发布了8 篇原创文章 · 获赞 0 · 访问量 128

猜你喜欢

转载自blog.csdn.net/ka530888/article/details/105187747
今日推荐