LoginSignup
0
0

More than 1 year has passed since last update.

Leetcode 744. Find Smallest Letter Greater Than Target

Last updated at Posted at 2022-11-30

744. Find Smallest Letter Greater Than Target

難易度

Easy

アプローチ

二分探索

class Solution {
    public char nextGreatestLetter(char[] letters, char target) {
        int letters_length = letters.length;
        int low = 0;
        int high = letters_length - 1;

        if (target < letters[low] || target >= letters[high]) {
            return letters[low];
        }
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (target < letters[mid]) {
                high = mid - 1;
            }
            if (letters[mid] <= target) {
                low = mid + 1;
            }
        }
        return letters[low];
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0