Java中计算整数中唯一数字数量的3种方法

给定一个整数,我们的目标是计算它包含多少个唯一数字。例如,整数 567890 有六个唯一数字,而 115577 只有三个唯一数字(1、5 和 7)。

1、使用集合
查找整数中唯一位数的最直接方法是使用Set。集合本质上消除了重复项,这使得它们非常适合我们的用例:

public static int countWithSet(int number) {
    number = Math.abs(number);
    Set<Character> uniqueDigits = new HashSet<>();
    String numberStr = String.valueOf(number);
    for (char digit : numberStr.toCharArray()) {
        uniqueDigits.add(digit);
    }
    return uniqueDigits.size();
}

让我们分解一下我们的算法步骤:
  • 将整数转换为字符串以轻松迭代每个数字。
  • 迭代字符串的每个字符并添加到HashSet中。
  • 迭代后HashSet的大小为我们提供了唯一数字的计数。

该解决方案的时间复杂度为O(n),其中n是整数的位数。添加到HashSet并检查其大小都是O(1)操作,但我们仍然需要迭代每个数字。

2、使用流API
Java 的Stream API提供了一种简洁而现代的解决方案来计算整数中唯一数字的数量。此方法利用流的强大功能以类似集合的方式处理元素序列,包括不同的元素:

public static long countWithStreamApi(int number) {
    return String.valueOf(Math.abs(number)).chars().distinct().count();
}
让我们检查一下所涉及的步骤:

  • 将数字转换为字符串。
  • 使用chars()方法从字符串中获取字符流。
  • 使用distinct()方法过滤掉重复的数字。
  • 使用count()方法获取唯一数字的数量。

时间复杂度与第一种方案相同。

3、使用位操作
让我们探索另一种解决方案。位操作还提供了一种跟踪唯一数字的方法:

public static int countWithBitManipulation(int number) {
    if (number == 0) {
        return 1;
    }
    number = Math.abs(number);
    int mask = 0;
    while (number > 0) {
        int digit = number % 10;
        mask |= 1 << digit;
        number /= 10;
    }
    return Integer.bitCount(mask);
}

这次我们的代码步骤如下:
  • 将整数掩码初始化为 0。掩码中的每一位将代表 0-9 中的一个数字。
  • 迭代数字的每个数字。
  • 对于每个数字,创建一个位表示。如果数字为 d,则位表示为 1 << d。
  • 使用按位或来更新mask。这将数字标记为所见。
  • 计算mask中设置为 1 的位数。该计数是唯一数字的数量。

时间复杂度也与上述解决方案相同。