Given a number as string, output the next number in order. The digits you may use are only those already given, plus any number of 0's.
Example:
Given 1234, output 1243;
Given 4321, output 10234.
Solution:
In previous post, I discussed the ways using C++ library function next_permutation. What if we are not allowed to use this function? Can we write de novo code?
First, I tried Java. Since string in Java cannot be updated in place, and hence it needs many auxiliary variables, including StringBuffer objects that can be set at a specific index.
Here is the Java code.
Secondly, I tried to implement this function in C. Suppose the string given was stored in a char array, which is the C style.
Here is the C code.
Related Posts:
Next Number in Order with next_permutation
No comments:
Post a Comment