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

前端 未结 4 750
你的背包
你的背包 2021-01-22 05:33

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 hasDistinctDigi

4条回答
  •  遥遥无期
    2021-01-22 05:43

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

提交回复
热议问题