leetcode H-Index II

3208 단어
제목 링크
코드를 엉망으로 썼다.눌러두다
public class Solution {
    public int hIndex(int[] citations) {
        if(citations.length==0)
        {
            return 0;
        }
        boolean allzearo=true;
        for (int i : citations) {
            if(i!=0)
            {
                allzearo=false;
            }
        }

        if(allzearo)
        {
            return 0;
        }

       int left=0;
        int n=citations.length;
        int right=n-1;
        int middle;
        int index=0;
        while(left<=right)
        {
            middle=(left+right)/2;
            if(citations[n-1-middle]>middle)
            {

                left=middle+1;
                index=middle;
            }
            else
            {
                right=middle-1;
            }

        }
        return index+1; 
    }
}

뉴맨 코드
int hIndex(int* citations, int citationsSize) {
    int lo = 0, hi = citationsSize, mid, index = 0;
    while (lo <= hi) {
        mid = lo + ((hi - lo) >> 1);
        if (citations[citationsSize - mid - 1] > mid) {
            lo = mid + 1;
            index = lo;
        } else {
            hi = mid - 1;
        }
    }
    return index;
}

좋은 웹페이지 즐겨찾기