Leetcode 744. 寻找比目标字母大的最小字母

文章目录

  • 题目链接
  • 代码
    • 二分查找

题目链接

744. 寻找比目标字母大的最小字母

代码

二分查找

char nextGreatestLetter(char* letters, int lettersSize, char target){
    int left = 0, right = lettersSize - 1;
    int ans = 0; // 1
    while (left <= right) {
        int mid = left + right >> 1;
        if (letters[mid] > target) {
            ans = mid;
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return letters[ans]; 
}
char nextGreatestLetter(char* letters, int lettersSize, char target){
    int left = 0, right = lettersSize - 1;
    char ans = letters[0];
    while (left <= right) {
        int mid = left + right >> 1;
        if (letters[mid] > target) {
            ans = letters[mid];
            right = mid - 1;
        } else {
            left = mid + 1;
        }
    }
    return ans; 
}

你可能感兴趣的:(LEETCODE,算法,leetcode,c语言)