Easiest and Understandable Solution✅ | Beats 100% in runtime🔥
Intuition\n Describe your first thoughts on how to solve this problem. \nReturning the smallest value by rearraning the dgits of the number,\n# Approach\n Descr
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nReturning the smallest value by rearraning the dgits of the number,\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Firstly, If given number is one digit number, return that number.\n- If the input number is negative, the function sets a flag neg to true, and then works with the absolute value of the input number.\n- The function converts the input number into a vector of strings, where each string represents a digit of the number.\n- The vector of strings is then sorted in ascending order.\n- If the input number was negative, the sorted vector is reversed to maintain the negative sign.\n- If the first digit of the sorted vector is 0, the function finds the first non-zero digit and swaps it with the leading 0. This step ensures that the resulting number does not have any leading zeros.\n- The function then reconstructs the number by iterating over the sorted vector of strings, converting each string back to an integer, and appending it to the result.\n- If the input number was negative, the final result is negated.\n\n![Screenshot 2024-05-09 232541.png](https://assets.leetcode.com/users/images/ffb9f194-e42b-4157-988d-6883e387451d_1715277379.1199665.png)\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(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        if(abs(num)<10) return num;\n        bool neg=false;\n        if(num<0) neg=true;\n        num=abs(num);\n        vector<string> number;\n        while(num){\n            number.push_back(to_string(num%10));\n            num/=10;\n        }\n        sort(number.begin(),number.end());\n        if(neg){\n            reverse(number.begin(),number.end());\n        }\n        if(number[0]=="0"){\n            for(int i=1;i<number.size();i++){\n                if(number[i]!="0"){\n                    string temp=number[i];\n                    number[i]="0";\n                    number[0]=temp;\n                    break;\n                }\n            }\n        }\n        for(int i=0;i<number.size();i++){\n            num*=10;\n            num+=stoi(number[i]);\n        }\n        if(neg) num*=-1;\n        return num;\n    }\n};\n```
['Math', 'Sorting', 'C++']