点名

某班级 n 位同学的学号为 0 ~ n-1。点名结果记录于升序数组 records。假定仅有一位同学缺席,请返回他的学号。

  • 输入: records = [0,1,2,3,5]
    输出: 4

  • 输入: records = [0, 1, 2, 3, 4, 5, 6, 8]
    输出: 7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
var takeAttendance = function(records) {
let left = 0
let right = records.length - 1
let mid = left + ((right - left) >> 1)
while(left <= right){
if(mid === records[mid]){
left = mid + 1
} else {
right = mid - 1
}
mid = left + ((right - left) >> 1)
}
return left
};