beats 100% | JAVA | easy to understand | O(n log n) time complexity
Complexity\n- Time complexity:O(n log n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(1)\n Add your space complexity here, e.g. O(n) \n\n
# Complexity\n- Time complexity:O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public long smallestNumber(long num) {\n\n        if(num==0){\n            return 0;\n        }\n\n        long result=Math.abs(num);\n        int freq[]=new int[10];\n        \n\n        while(result>0){\n            int temp=(int)(result%10);\n            freq[temp]++;\n            result/=10;\n        }\n\n        if(num>0){\n\n            int i=1;\n            int count_zero=freq[0];\n\n            while(freq[i]==0){\n                i++;\n            }\n\n            result=(result*10)+i;\n            freq[i]--;\n\n            while(count_zero-->0){\n                result=(result*10);\n            }\n\n            while(i<10){\n\n                while(freq[i]>0){\n                    result=(result*10)+i;\n                    freq[i]--;\n                }\n\n                i++;\n\n            }\n        }else{\n\n            int i=9;\n        \n            while(i>=0){\n\n                while(freq[i]>0){\n                    result=(result*10)+i;\n                    freq[i]--;\n                }\n\n                i--;\n\n            }\n            result*=-1;\n\n        }\n\n        return result; \n        \n    }\n}\n```
['Java']