返回介绍

solution / 2100-2199 / 2165.Smallest Value of the Rearranged Number / README_EN

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

2165. Smallest Value of the Rearranged Number

中文文档

Description

You are given an integer num. Rearrange the digits of num such that its value is minimized and it does not contain any leading zeros.

Return _the rearranged number with minimal value_.

Note that the sign of the number does not change after rearranging the digits.

 

Example 1:

Input: num = 310
Output: 103
Explanation: The possible arrangements for the digits of 310 are 013, 031, 103, 130, 301, 310. 
The arrangement with the smallest value that does not contain any leading zeros is 103.

Example 2:

Input: num = -7605
Output: -7650
Explanation: Some possible arrangements for the digits of -7605 are -7650, -6705, -5076, -0567.
The arrangement with the smallest value that does not contain any leading zeros is -7650.

 

Constraints:

  • -1015 <= num <= 1015

Solutions

Solution 1

class Solution:
  def smallestNumber(self, num: int) -> int:
    if num == 0:
      return 0
    cnt = [0] * 10
    neg = num < 0
    num = abs(num)
    while num:
      num, v = divmod(num, 10)
      cnt[v] += 1
    ans = ""
    if neg:
      for i in range(9, -1, -1):
        if cnt[i]:
          ans += str(i) * cnt[i]
      return -int(ans)
    if cnt[0]:
      for i in range(1, 10):
        if cnt[i]:
          ans += str(i)
          cnt[i] -= 1
          break
    for i in range(10):
      if cnt[i]:
        ans += str(i) * cnt[i]
    return int(ans)
class Solution {
  public long smallestNumber(long num) {
    if (num == 0) {
      return 0;
    }
    int[] cnt = new int[10];
    boolean neg = num < 0;
    num = Math.abs(num);
    while (num != 0) {
      cnt[(int) (num % 10)]++;
      num /= 10;
    }
    long ans = 0;
    if (neg) {
      for (int i = 9; i >= 0; --i) {
        while (cnt[i]-- > 0) {
          ans = ans * 10 + i;
        }
      }
      return -ans;
    }
    if (cnt[0] > 0) {
      for (int i = 1; i < 10; ++i) {
        if (cnt[i] > 0) {
          ans = ans * 10 + i;
          cnt[i]--;
          break;
        }
      }
    }
    for (int i = 0; i < 10; ++i) {
      while (cnt[i]-- > 0) {
        ans = ans * 10 + i;
      }
    }
    return ans;
  }
}
class Solution {
public:
  long long smallestNumber(long long num) {
    if (num == 0) return 0;
    vector<int> cnt(10);
    bool neg = num < 0;
    num = abs(num);
    while (num) {
      cnt[num % 10]++;
      num /= 10;
    }
    long long ans = 0;
    if (neg) {
      for (int i = 9; i >= 0; --i)
        while (cnt[i]--) ans = ans * 10 + i;
      return -ans;
    }
    if (cnt[0]) {
      for (int i = 1; i < 10; ++i) {
        if (cnt[i]) {
          ans = ans * 10 + i;
          cnt[i]--;
          break;
        }
      }
    }
    for (int i = 0; i < 10; ++i)
      while (cnt[i]--) ans = ans * 10 + i;
    return ans;
  }
};
func smallestNumber(num int64) int64 {
  if num == 0 {
    return 0
  }
  cnt := make([]int, 10)
  neg := num < 0
  if neg {
    num = -num
  }
  for num != 0 {
    cnt[num%10]++
    num /= 10
  }
  ans := 0
  if neg {
    for i := 9; i >= 0; i-- {
      for j := 0; j < cnt[i]; j++ {
        ans = ans*10 + i
      }
    }
    return -int64(ans)
  }
  if cnt[0] > 0 {
    for i := 1; i < 10; i++ {
      if cnt[i] > 0 {
        ans = ans*10 + i
        cnt[i]--
        break
      }
    }
  }
  for i := 0; i < 10; i++ {
    for j := 0; j < cnt[i]; j++ {
      ans = ans*10 + i
    }
  }
  return int64(ans)
}

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

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

发布评论

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