检查整数是否有重复数字。没有字符串方法或数组 [英] Check if integer has repeating digits. No string methods or arrays

查看:178
本文介绍了检查整数是否有重复数字。没有字符串方法或数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查看int是否具有相同数字的倍数。尝试不使用字符串方法或数组。我遇到问题的主要方法是 hasDistinctDigits()。当重复数字位于结尾时,它会起作用,但不会在它们位于开头或中间时起作用。

I'm trying to see if an int has multiples of the same digit. Trying to do it without string methods or arrays. The main method I'm having trouble with is hasDistinctDigits(). It works when the repeating digits are at the end, but not when they come at the beginning or middle.

public static void main(String[] args) {
    System.out.println(hasDistinctDigits(12234));
}

public static boolean hasDistinctDigits(int number) {
    boolean returner = true;
    int count = 1;
    int newNum = number;
    int digit = 0;

    while (count < numDigits(number)) {         
        while (count < numDigits(newNum)) {
            digit = newNum % 10;
            newNum/=10;
            if (digit == getDigit(newNum, count)) {
                returner = false;
            }
            count++;                
        }
        count++;
    }
    return returner;
}

public static int numDigits(int number) {
    int count = 0;
    while (number != 0) {
        number /= 10;
        count++;
    }
    return count;
}

public static int getDigit(int number, int i) {
    int digit = 0;
    int count = 0;
    int originalNum = number;

    while (count <= i) {
        if (count == i) {
            digit = number % 10;
        }
        number /= 10;
        count++;
    }
    if (i > numDigits(originalNum)) {
        return -1;
    } else {
        return digit;
    }
}

}

如果这样运行,你会看到'2'重复自身,但是当它应该是时,该方法仍然计算为 true

If this is run, you will see '2' repeats itself, but the method still evaluates to true when it should be false.

推荐答案

这是一个简短而甜蜜的版本:)

Here is a short and sweet version :)

 private static boolean hasDistinctDigits(int number) {
     int numMask = 0;
     int numDigits = (int) Math.ceil(Math.log10(number+1));
     for (int digitIdx = 0; digitIdx < numDigits; digitIdx++) {
         int curDigit = (int)(number / Math.pow(10,digitIdx)) % 10;
         int digitMask = (int)Math.pow(2, curDigit);             
         if ((numMask & digitMask) > 0) return false;
         numMask = numMask | digitMask;
     }
     return true;
 }

它以非常简单的方式工作。 numMask 是一个整数,用于存储已遇到的数字(因为十进制系统编号只有10位数,一个整数使用16位,我们有足够的位存储每个十进制数字。)

It works in a pretty simply way. numMask is an integer used to store what digits have already been encountered (since a decimal system number has only 10 digits and an integer gives use 16-bits, we have enough bits to store each decimal digit as it occurs).

我们遍历数字中的所有数字。对于每个数字索引,我们得到 curDigit 中的实际数字。假设当前数字是 5 。然后我们检查它的第5位是在numMask中引出的:如果是,那么我们过去已经遇到过 5 ,所以我们可以立即告诉这个数字是没有所有不同的数字并返回false;否则,我们修改 numMask 并提高第5位。

We loop over all digits in the number. For each digit index, we get the actual digit in curDigit. Let's say the current digit is 5. We then check it the 5th bit is raised in out numMask: if it is, then we have already encounter a 5 in the past, so we can immediately tell that the number does not have all distinct digits and return false; otherwise, we modify numMask and raise the 5th bit.

如果我们将它推到最后,那么就不会重复遇到了数字。

If we make it to the end, then no dupicate digits were encountered.

这篇关于检查整数是否有重复数字。没有字符串方法或数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆