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

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

问题描述

我正在尝试查看 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,而本应为 false.

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.然后我们检查它在 numMask 中的第 5 位:如果是,那么我们过去已经遇到了 5,因此我们可以立即判断该数字没有所有不同的数字,并且返回假;否则,我们修改 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天全站免登陆