Hello, for the Look And Say problem chapter 7.6, I feel that the following reccursive solution is better because it’s time complexity is better O(n) (?) where n is the number of digit and it is also more elegant (?)
public class LookAndSay {
public int nth(int number) {
if (number < 0)
return 0;
int digit = number % 10;
boolean exit = number < 10;
number /= 10;
if (exit) {
return 10 + digit;
}
int val = nth(number);
if (val % 10 == digit) {
return 10 + val;
}
else {
return val * 100 + 10 + digit;
}
}
public static void main(String[] args) {
System.out.println("Hello");
LookAndSay lookAndSay = new LookAndSay();
System.out.println("" + lookAndSay.nth(11223344));
}
}
Let me know what you think…
Thanks
Shan