lc274 發表於 2016-01-30 | 分類於 leetcode | 0 comments | 12345678910111213141516171819202122class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ N = len(citations) d = [] t = 0 for x in range(N+1): d.append(0) for x in citations: if x <= N: d[x] += 1 else: t += 1 for x in range(N+1): xx = N - x t += d[xx] if t >= xx: return xx return -1 123456789101112131415161718192021222324252627class Solution(object): def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ N = len(citations) if N == 0: return 0 citations.sort(reverse = True) mid = N / 2 begin = 0 end = N - 1 while True: if begin >= end: if citations[mid] >= mid + 1: return mid + 1 else: return mid if citations[mid] < mid + 1: end = mid - 1 mid = (begin + end) / 2 elif citations[mid] > mid + 1: begin = mid + 1 mid = (begin + end) / 2 else: return mid + 1