Backtracking
1 | class Solution { |
Remarks:
- TC: $O(n!)$, SC: $O(n)$.
- Java string:
"124" + 1 = "1241"
- State to be saved as parameters: (a) current string, (b) used nums
Math
1 | class Solution { |
Remarks:
- TC: $O(n^2)$ (everytime we remove a number
idx
from the array, it takes $O(n)$), SC: $O(n)$. - Idea:
Assume all permutations is $n!$, then the permutation of each numbers being placed at the first position is $(n-1)!$. So we can detemine the numbers digit by digit, from left to right.