I have this method:
private String computePerm(int iteration) {
if (iteration < n + 1) {
return Character.toString((char) (iteration + 48));
} else {
if (iteration % n == 0) {
return computePerm((iteration / n) - 1) + computePerm(((iteration - 1) % n + 1));
} else {
return computePerm(iteration / n) + computePerm(iteration % n);
}
}
}
It computes a permutation induced by a single breadth-first search traversal. I'm using it to solve Post's correspondence problem . However, I suspect it is tail-recursive, and it seems to incur an ugly overhead on some instances of the problem.
How can I remove tail-recursion while preserving the method's behavior?
This does what you're after (the permutation of an alphabet of size n ):
private static String computePerm(int iteration) {
return Integer.toString(iteration, n);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.