247. Strobogrammatic Number II #305
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.

Resolves #57
Algorithm:
We could think of a strobogrammatic number as another kind palindrome.
From the question statement, we already know the length of this number. So, in order to build this number, we can create a character array. In this char array, we put the reflective digits in the opposite sides of the array.
For example, in an array of length 5, if we put 1 on the index 0, we put another one on the index 4 (n-1). After that, we put a 6 on the index 1 (previous left+1) and then the corresponding digit 9 on the index 3 (n-2, previous right+1). At the end, when we continue the pattern both left and right becomes 2. We then put digits that are suitable for a middle reflection (0, 1, 8).
This pattern also holds when modified a bit, for even lengths:
At even lengths such as 4, left will eventually assume the index value 2 and right the index value 1 (left>right). Then, we shall not add anything into the character array.
Only thing we should also consider is that we shouldn't place '0' at the front when left is 0, that would not be a valid number.
We can also put the reflective number pairs in char[][] and easily loop over them.