返回介绍

solution / 0300-0399 / 0357.Count Numbers with Unique Digits / README_EN

发布于 2024-06-17 01:04:01 字数 5247 浏览 0 评论 0 收藏 0

357. Count Numbers with Unique Digits

中文文档

Description

Given an integer n, return the count of all numbers with unique digits, x, where 0 <= x < 10n.

 

Example 1:

Input: n = 2
Output: 91
Explanation: The answer should be the total numbers in the range of 0 ≤ x < 100, excluding 11,22,33,44,55,66,77,88,99

Example 2:

Input: n = 0
Output: 1

 

Constraints:

  • 0 <= n <= 8

Solutions

Solution 1

class Solution:
  def countNumbersWithUniqueDigits(self, n: int) -> int:
    if n == 0:
      return 1
    if n == 1:
      return 10
    ans, cur = 10, 9
    for i in range(n - 1):
      cur *= 9 - i
      ans += cur
    return ans
class Solution {
  public int countNumbersWithUniqueDigits(int n) {
    if (n == 0) {
      return 1;
    }
    if (n == 1) {
      return 10;
    }
    int ans = 10;
    for (int i = 0, cur = 9; i < n - 1; ++i) {
      cur *= (9 - i);
      ans += cur;
    }
    return ans;
  }
}
class Solution {
public:
  int countNumbersWithUniqueDigits(int n) {
    if (n == 0) return 1;
    if (n == 1) return 10;
    int ans = 10;
    for (int i = 0, cur = 9; i < n - 1; ++i) {
      cur *= (9 - i);
      ans += cur;
    }
    return ans;
  }
};
func countNumbersWithUniqueDigits(n int) int {
  if n == 0 {
    return 1
  }
  if n == 1 {
    return 10
  }
  ans := 10
  for i, cur := 0, 9; i < n-1; i++ {
    cur *= (9 - i)
    ans += cur
  }
  return ans
}

Solution 2

class Solution:
  def countNumbersWithUniqueDigits(self, n: int) -> int:
    @cache
    def dfs(pos, mask, lead):
      if pos <= 0:
        return 1
      ans = 0
      for i in range(10):
        if (mask >> i) & 1:
          continue
        if i == 0 and lead:
          ans += dfs(pos - 1, mask, lead)
        else:
          ans += dfs(pos - 1, mask | (1 << i), False)
      return ans

    return dfs(n, 0, True)
class Solution {
  private int[][] dp = new int[10][1 << 11];

  public int countNumbersWithUniqueDigits(int n) {
    for (var e : dp) {
      Arrays.fill(e, -1);
    }
    return dfs(n, 0, true);
  }

  private int dfs(int pos, int mask, boolean lead) {
    if (pos <= 0) {
      return 1;
    }
    if (!lead && dp[pos][mask] != -1) {
      return dp[pos][mask];
    }
    int ans = 0;
    for (int i = 0; i < 10; ++i) {
      if (((mask >> i) & 1) == 1) {
        continue;
      }
      if (i == 0 && lead) {
        ans += dfs(pos - 1, mask, lead);
      } else {
        ans += dfs(pos - 1, mask | (1 << i), false);
      }
    }
    if (!lead) {
      dp[pos][mask] = ans;
    }
    return ans;
  }
}
class Solution {
public:
  int dp[10][1 << 11];

  int countNumbersWithUniqueDigits(int n) {
    memset(dp, -1, sizeof dp);
    return dfs(n, 0, true);
  }

  int dfs(int pos, int mask, bool lead) {
    if (pos <= 0) {
      return 1;
    }
    if (!lead && dp[pos][mask] != -1) {
      return dp[pos][mask];
    }
    int ans = 0;
    for (int i = 0; i < 10; ++i) {
      if ((mask >> i) & 1) continue;
      if (i == 0 && lead) {
        ans += dfs(pos - 1, mask, lead);
      } else {
        ans += dfs(pos - 1, mask | 1 << i, false);
      }
    }
    if (!lead) {
      dp[pos][mask] = ans;
    }
    return ans;
  }
};
func countNumbersWithUniqueDigits(n int) int {
  dp := make([][]int, 10)
  for i := range dp {
    dp[i] = make([]int, 1<<11)
    for j := range dp[i] {
      dp[i][j] = -1
    }
  }
  var dfs func(int, int, bool) int
  dfs = func(pos, mask int, lead bool) int {
    if pos <= 0 {
      return 1
    }
    if !lead && dp[pos][mask] != -1 {
      return dp[pos][mask]
    }
    ans := 0
    for i := 0; i < 10; i++ {
      if ((mask >> i) & 1) == 1 {
        continue
      }
      if i == 0 && lead {
        ans += dfs(pos-1, mask, lead)
      } else {
        ans += dfs(pos-1, mask|1<<i, false)
      }
    }
    if !lead {
      dp[pos][mask] = ans
    }
    return ans
  }

  return dfs(n, 0, true)
}

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

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

发布评论

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