classSolution { publicinthIndex(int[] citations) { intn= citations.length; intleft=0, right = n - 1; while (left <= right) { intmid= left + (right - left) / 2; if (citations[mid] >= n - mid) { right = mid - 1; } else { left = mid + 1; } } return n - left; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
publicclassSolution { publicintHIndex(int[] citations) { int n = citations.Length; int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (citations[mid] >= n - mid) { right = mid - 1; } else { left = mid + 1; } } return n - left; } }
[sol1-Python3]
1 2 3 4 5 6 7 8 9 10 11
classSolution: defhIndex(self, citations: List[int]) -> int: n = len(citations) left = 0; right = n - 1 while left <= right: mid = left + (right - left) // 2 if citations[mid] >= n - mid: right = mid - 1 else: left = mid + 1 return n - left
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13
var hIndex = function(citations) { let n = citations.length; let left = 0, right = n - 1; while (left <= right) { const mid = left + Math.floor((right - left) / 2); if (citations[mid] >= n - mid) { right = mid - 1; } else { left = mid + 1; } } return n - left; };
[sol1-C++]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: inthIndex(vector<int>& citations){ int n = citations.size(); int left = 0, right = n - 1; while (left <= right) { int mid = left + (right - left) / 2; if (citations[mid] >= n - mid) { right = mid - 1; } else { left = mid + 1; } } return n - left; } };
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12
inthIndex(int* citations, int citationsSize) { int left = 0, right = citationsSize - 1; while (left <= right) { int mid = left + (right - left) / 2; if (citations[mid] >= citationsSize - mid) { right = mid - 1; } else { left = mid + 1; } } return citationsSize - left; }
[sol1-Golang]
1 2 3 4
funchIndex(citations []int)int { n := len(citations) return n - sort.Search(n, func(x int)bool { return citations[x] >= n-x }) }