java - Check if integer has repeating digits. No string methods or arrays -
I am trying to see if there are multiples of the same number in an int without string methods or arrays The main method I'm trying to do is having trouble with this hedistentdegit ()
. This works when repetitive points occur in the end, but when they do not come in the beginning or middle.
public static zero main (string [] args) {System.out.println (hasDistinctDigits (12234)); } Public stable boolean handicid Digits (int number) {Boolean ritter = true; Int count = 1; Int newNum = number; Int digit = 0; While (count> numDigits (number)) {while (count> numDigits (newNum)) {points = newon% 10; NewNum / = 10; If (points == getDigit (new name, count)) {returner = false; } Calculate ++; } Calculate ++; } Return returner; } Public static int numbers (numeric digits) {int count = 0; While (number! = 0) {number / = 10; Count ++; } Calculation of return; } Public Fixed int getDigit (int number, int i) {int number = 0; Int count = 0; Int element = number; While (calculation & lt; = i) {if (count == i) {points = number% 10; } Number = / 10; Count ++; } If (i> Numeric (Num)) {return -1; } And {return points; }}
}
} If this is going on, you will see '2' repeats itself, but the method is still true
It evaluates to be false
.
There is a small and sweet version here :)
Private static Boolean diagnostics (int number) {int nummask = 0; Int numDigits = (int) Math.ceil (Math.log10 (number + 1)); (Int digiDX = 0; digiticleindex & lt; numdigits; digitIdx ++) {int verdegate = (int) (number / MathPo (10, numeric x)% 10; Intimate DigitMask = (int) MathPo (2, Cordigate); If ((nummask and digitmask)> 0) wrong return; Nummask = nummask | DigitMask; } Back true; }
This works in a very simple way. nummask
is an integer that already gets the data (because the decimal system number contains only 10 digits and an integer uses 16-bits, we have to store every decimal digit There are enough bits).
For the loop for each digit index on all the digits in the number, we get the actual number in the curdigit
. Let's say the current score is 5
. After this we check it that the fifth bit has been exited: if it is, then we have already encountered a 5
, so we can immediately tell that all in the number There are no specific points and the details are false; Otherwise, we modify the nummask
and increase the 5th bit.
If we make it to the end, then no duplicate points are found.
Comments
Post a Comment