Discuss / Java / 交作业

交作业

Topic source

海乙那6193

#1 Created at ... [Delete] [Delete and Lock User]
static int findMissingNumber(int start, int end, List<Integer> list) {    for (int i=0;i< list.size();i++){        if (list.get(i) - start != i){            return start+i;        }    }    return -1;}

海乙那6193

#2 Created at ... [Delete] [Delete and Lock User]

foreach版:

static int findMissingNumber(int start, int end, List<Integer> list) {

    int i = 0;

    for (Integer n:list){

        if(n-start != i){

            return n-1;

        }

        i++;

    }

    return -1;

}

海乙那6193

#3 Created at ... [Delete] [Delete and Lock User]

static int findMissingNumber(int start, int end, List<Integer> list) {

       int total = (start + end) * (end - start +1)/2;

       int sum = 0;

       for (int n:list){

           sum +=n;

       }

        return total-sum;

    }


  • 1

Reply