123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- package main
- import (
- "bufio"
- "fmt"
- "os"
- "strconv"
- "strings"
- )
- func main() {
- var input = bufio.NewScanner(os.Stdin)
- var output int
- var digits []int
- for input.Scan() {
- line := input.Text()
- digits = []int{}
- values := strings.Split(line, " ")
- for _, v := range values {
- d, _ := strconv.Atoi(v)
- digits = append(digits, d)
- }
- output = missingNumber(digits)
- fmt.Println(output)
- }
- }
- func missingNumber(nums []int) int {
- var length = len(nums)
- if length-1 == nums[length-1] {
- return length
- }
- var left, right = 0, len(nums) - 1
- var mid int
- for left < right {
- mid = (left + right) / 2
- if nums[mid] > mid {
- right = mid
- } else {
- left = mid + 1
- }
- }
- return left
- }
|