返回介绍

solution / 2200-2299 / 2259.Remove Digit From Number to Maximize Result / README_EN

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

2259. Remove Digit From Number to Maximize Result

中文文档

Description

You are given a string number representing a positive integer and a character digit.

Return _the resulting string after removing exactly one occurrence of _digit_ from _number_ such that the value of the resulting string in decimal form is maximized_. The test cases are generated such that digit occurs at least once in number.

 

Example 1:

Input: number = "123", digit = "3"
Output: "12"
Explanation: There is only one '3' in "123". After removing '3', the result is "12".

Example 2:

Input: number = "1231", digit = "1"
Output: "231"
Explanation: We can remove the first '1' to get "231" or remove the second '1' to get "123".
Since 231 > 123, we return "231".

Example 3:

Input: number = "551", digit = "5"
Output: "51"
Explanation: We can remove either the first or second '5' from "551".
Both result in the string "51".

 

Constraints:

  • 2 <= number.length <= 100
  • number consists of digits from '1' to '9'.
  • digit is a digit from '1' to '9'.
  • digit occurs at least once in number.

Solutions

Solution 1

class Solution:
  def removeDigit(self, number: str, digit: str) -> str:
    return max(
      number[:i] + number[i + 1 :] for i, d in enumerate(number) if d == digit
    )
class Solution {
  public String removeDigit(String number, char digit) {
    String ans = "0";
    for (int i = 0, n = number.length(); i < n; ++i) {
      char d = number.charAt(i);
      if (d == digit) {
        String t = number.substring(0, i) + number.substring(i + 1);
        if (ans.compareTo(t) < 0) {
          ans = t;
        }
      }
    }
    return ans;
  }
}
class Solution {
public:
  string removeDigit(string number, char digit) {
    string ans = "0";
    for (int i = 0, n = number.size(); i < n; ++i) {
      char d = number[i];
      if (d == digit) {
        string t = number.substr(0, i) + number.substr(i + 1, n - i);
        if (ans < t) {
          ans = t;
        }
      }
    }
    return ans;
  }
};
func removeDigit(number string, digit byte) string {
  ans := "0"
  for i, d := range number {
    if d == rune(digit) {
      t := number[:i] + number[i+1:]
      if strings.Compare(ans, t) < 0 {
        ans = t
      }
    }
  }
  return ans
}
function removeDigit(number: string, digit: string): string {
  const n = number.length;
  let last = -1;
  for (let i = 0; i < n; ++i) {
    if (number[i] === digit) {
      last = i;
      if (i + 1 < n && number[i] < number[i + 1]) {
        break;
      }
    }
  }
  return number.substring(0, last) + number.substring(last + 1);
}
class Solution {
  /**
   * @param String $number
   * @param String $digit
   * @return String
   */
  function removeDigit($number, $digit) {
    $max = 0;
    for ($i = 0; $i < strlen($number); $i++) {
      if ($number[$i] == $digit) {
        $tmp = substr($number, 0, $i) . substr($number, $i + 1);
        if ($tmp > $max) {
          $max = $tmp;
        }
      }
    }
    return $max;
  }
}

Solution 2

class Solution:
  def removeDigit(self, number: str, digit: str) -> str:
    last = -1
    n = len(number)
    for i, d in enumerate(number):
      if d == digit:
        last = i
        if i + 1 < n and d < number[i + 1]:
          break
    return number[:last] + number[last + 1 :]
class Solution {
  public String removeDigit(String number, char digit) {
    int last = -1;
    int n = number.length();
    for (int i = 0; i < n; ++i) {
      char d = number.charAt(i);
      if (d == digit) {
        last = i;
        if (i + 1 < n && d < number.charAt(i + 1)) {
          break;
        }
      }
    }
    return number.substring(0, last) + number.substring(last + 1);
  }
}
class Solution {
public:
  string removeDigit(string number, char digit) {
    int n = number.size();
    int last = -1;
    for (int i = 0; i < n; ++i) {
      char d = number[i];
      if (d == digit) {
        last = i;
        if (i + 1 < n && number[i] < number[i + 1]) {
          break;
        }
      }
    }
    return number.substr(0, last) + number.substr(last + 1);
  }
};
func removeDigit(number string, digit byte) string {
  last := -1
  n := len(number)
  for i := range number {
    if number[i] == digit {
      last = i
      if i+1 < n && number[i] < number[i+1] {
        break
      }
    }
  }
  return number[:last] + number[last+1:]
}

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

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

发布评论

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