简体   繁体   English

检查整数是否有重复的数字。 没有字符串方法或数组

[英]Check if integer has repeating digits. No string methods or arrays

I'm trying to see if an int has multiples of the same digit. 我正在尝试查看int是否具有相同数字的倍数。 Trying to do it without string methods or arrays. 尝试不使用字符串方法或数组。 The main method I'm having trouble with is hasDistinctDigits() . 我遇到麻烦的主要方法是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;
    }
}

} }

If this is run, you will see '2' repeats itself, but the method still evaluates to true when it should be false . 如果运行时,你会看到“2”重演,但该方法仍评估为true时,它应该是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;
 }

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). numMask是一个整数,用于存储已经遇到的数字(由于十进制系统编号只有10位,而整数为16位,因此我们有足够的位数来存储出现的每个十进制数字)。

We loop over all digits in the number. 我们遍历数字中的所有数字。 For each digit index, we get the actual digit in curDigit . 对于每个数字索引,我们在curDigit获得实际数字。 Let's say the current digit is 5 . 假设当前数字为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; 然后,我们检查它是否将numMask的第5位提高了:如果是,那么我们已经在过去遇到过5 ,因此我们可以立即知道该数字没有所有不同的数字,并返回false;否则,返回false。 otherwise, we modify numMask and raise the 5th bit. 否则,我们修改numMask并提高第5位。

If we make it to the end, then no dupicate digits were encountered. 如果我们进行到最后,则不会遇到重复的数字。

You need to check each digit with every other digit. 您需要将每个数字与其他每个数字进行检查。 This suggests that you should have at least two nested loops. 这表明您应该至少有两个嵌套循环。 You seem to have mixed them both. 您似乎混合了两者。

Have one loop for the digit being checked and other for iterating over all other digits. 有一个循环用于检查该数字,另一循环用于遍历所有其他数字。


Also, your getDigit method is not working correctly. 另外,您的getDigit方法无法正常工作。 Replace it with 替换为

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;
    }
}

Hope this helps. 希望这可以帮助。 Good luck. 祝好运。

Same logic to verify if a string has unique characters can be used here. 可以在此处使用相同的逻辑来验证字符串是否具有唯一字符。 (1 << currentChar) , it sets the bit to 1 in currentChar equals to a number(0-9) present at that index and all other bits are set to 0. (1 << currentChar),它将currentChar中的位设置为1,等于该索引处的数字(0-9),所有其他位设置为0。

(result &(1 << currentChar) : If bit is already set to 1 then return false else (结果&(1 << currentChar):如果位已经设置为1,则返回false

result = result|(1 << currentChar): Set the bit in result integer which is equal to the number at that index. result = result |(1 << currentChar):将结果整数中的位设置为等于该索引处的数字。

public class CheckIfDigitsAreRepeated {


        public static void main(String[] args) {
            int input = 1234567897; // false
            // int input = 1234567890;  true

            System.out.println(hasDistinctDigits(input));
        }

        public static boolean hasDistinctDigits(int input){
            int result = 0;

            String inputString = String.valueOf(input);


            for (int i=0; i < inputString.length();i++){

                int currentChar = inputString.charAt(i)- '1';

                if((result &(1 << currentChar)) > 0){
                    return false;
                }

                result = result|(1 << currentChar);

        }

            return true;
        }
    }
public static boolean hasDistinctDigits(int number) {
         int numMask = Math.floorMod(number, 10);
         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;
             if(numMask != curDigit)  return false;
             numMask = numMask & curDigit;
         }
         return true;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM