美文网首页
Missing Number(Binary Search)

Missing Number(Binary Search)

作者: GakkiLove | 来源:发表于2018-04-22 20:32 被阅读0次

Given an integer array of size N - 1 sorted by ascending order, containing all the numbers from 1 to N except one, find the missing number.

Assumptions

The given array is not null, and N >= 1

Examples

A = {1, 2, 4}, the missing number is 3
A = {1, 2, 3}, the missing number is 4
A = {}, the missing number is 1

class Solution(object):
  def missing(self, array):
    low = 0
    high = len(array) - 1
    if len(array) < 1:
      return 1
    while low <= high:
      mid = (low + high)/2
      for i in xrange(0,len(array)):
        if array[i] != i + 1:
          return i+1
        else:
          i += 1
      return array[-1] + 1

相关文章

网友评论

      本文标题:Missing Number(Binary Search)

      本文链接:https://www.haomeiwen.com/subject/mnaclftx.html