返回介绍

solution / 0700-0799 / 0744.Find Smallest Letter Greater Than Target / README_EN

发布于 2024-06-17 01:03:34 字数 5722 浏览 0 评论 0 收藏 0

744. Find Smallest Letter Greater Than Target

中文文档

Description

You are given an array of characters letters that is sorted in non-decreasing order, and a character target. There are at least two different characters in letters.

Return _the smallest character in _letters_ that is lexicographically greater than _target. If such a character does not exist, return the first character in letters.

 

Example 1:

Input: letters = ["c","f","j"], target = "a"
Output: "c"
Explanation: The smallest character that is lexicographically greater than 'a' in letters is 'c'.

Example 2:

Input: letters = ["c","f","j"], target = "c"
Output: "f"
Explanation: The smallest character that is lexicographically greater than 'c' in letters is 'f'.

Example 3:

Input: letters = ["x","x","y","y"], target = "z"
Output: "x"
Explanation: There are no characters in letters that is lexicographically greater than 'z' so we return letters[0].

 

Constraints:

  • 2 <= letters.length <= 104
  • letters[i] is a lowercase English letter.
  • letters is sorted in non-decreasing order.
  • letters contains at least two different characters.
  • target is a lowercase English letter.

Solutions

Solution 1

class Solution:
  def nextGreatestLetter(self, letters: List[str], target: str) -> str:
    left, right = 0, len(letters)
    while left < right:
      mid = (left + right) >> 1
      if ord(letters[mid]) > ord(target):
        right = mid
      else:
        left = mid + 1
    return letters[left % len(letters)]
class Solution {
  public char nextGreatestLetter(char[] letters, char target) {
    int left = 0, right = letters.length;
    while (left < right) {
      int mid = (left + right) >> 1;
      if (letters[mid] > target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return letters[left % letters.length];
  }
}
class Solution {
public:
  char nextGreatestLetter(vector<char>& letters, char target) {
    int left = 0, right = letters.size();
    while (left < right) {
      int mid = left + right >> 1;
      if (letters[mid] > target) {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    return letters[left % letters.size()];
  }
};
func nextGreatestLetter(letters []byte, target byte) byte {
  left, right := 0, len(letters)
  for left < right {
    mid := (left + right) >> 1
    if letters[mid] > target {
      right = mid
    } else {
      left = mid + 1
    }
  }
  return letters[left%len(letters)]
}
function nextGreatestLetter(letters: string[], target: string): string {
  const n = letters.length;
  let left = 0;
  let right = letters.length;
  while (left < right) {
    let mid = (left + right) >>> 1;
    if (letters[mid] > target) {
      right = mid;
    } else {
      left = mid + 1;
    }
  }
  return letters[left % n];
}
impl Solution {
  pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
    *letters
      .iter()
      .find(|&&c| c > target)
      .unwrap_or(&letters[0])
  }
}
class Solution {
  /**
   * @param String[] $letters
   * @param String $target
   * @return String
   */
  function nextGreatestLetter($letters, $target) {
    $left = 0;
    $right = count($letters);
    while ($left <= $right) {
      $mid = floor($left + ($right - $left) / 2);
      if ($letters[$mid] > $target) {
        $right = $mid - 1;
      } else {
        $left = $mid + 1;
      }
    }
    if ($left >= count($letters)) {
      return $letters[0];
    } else {
      return $letters[$left];
    }
  }
}

Solution 2

impl Solution {
  pub fn next_greatest_letter(letters: Vec<char>, target: char) -> char {
    let n = letters.len();
    let mut left = 0;
    let mut right = n;
    while left < right {
      let mid = left + (right - left) / 2;
      if letters[mid] > target {
        right = mid;
      } else {
        left = mid + 1;
      }
    }
    letters[left % n]
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文