Returns the length of the longest sequence of identical digits found in num recursive
I had an assignement;
"Write a recursive static method that accepts a positive integer num and returns the length of the longest sequence of identical digits found in num."
This is the solution I did
public static int equalDigits (int num)
{
return equalDigits(num,1,0);
}
private static int equalDigits (int num, int count, int max){
if (num < 10)
return 1;
if (num/10%10 == num%10)
count++;
else
count = 1;
max = count > max ? count : max;
return Math.max(max,equalDigits(num/10,count,max));
}
But my lecturer said it can be done without using helper method. I can't figured how exactly it's possible.
Comments
Post a Comment