# My easy understood java solution

• We can find some trends by checking the numbers.

0-8 [9] total 10 - 1
0* - 8* [9*] total 100 - 1 - 10
0** - 8** [9**] total 1000 - (1 + 10) * 9 - 100
... total 10^n - (1 + 10 + ...) * 9 - 10^(n - 1)

for every n, we can calculate the max number under 10^n and save in helper array.

In the next step, for every given number, we can calculate digit at nth position in the following way:
if number value is larger then helper[n], this means the digit in the nth position is int(number/helper[n]).
Then we subtract the these values from number and check whether the number is larger than helper[n - 1] until the last digit.

``````public class Solution {
public int newInteger(int n) {
long[] helper = new long[10];
long count = 1;
long m = 10;
helper[0] = 1;
for (int i = 1; i < 10; i++) {
helper[i] = m - count;
count = m + count * 9;
m *= 10;
}

count = 0;
for (int i = 9; i >= 1; i--) {
if (n >= helper[i]) {
int temp = n / (int) helper[i];
n = n - temp * (int) helper[i];
count = temp + count;
}
count = count * 10;
}
count = count + n;
return (int) count;
}
}
``````

• Can you please explain the logic?

• @SanD I add the explanations as above.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.