[C++] Sort Based on Positive or Negative Number
Not sure if this is the most optimal way, but here\'s an approach. Given the input num, we notice that if the number is negative, we want to return the number w
Not sure if this is the most optimal way, but here\'s an approach. Given the input `num`, we notice that if the number is negative, we want to return the number with digits in descending order. If the number is positive, sort by ascending order, and then find the first non-zero digit\'s index and swap it with the first zero.\n\n```cpp\nclass Solution {\npublic:\n    long long smallestNumber(long long num) {\n        if (num == 0) {\n            return 0;\n        }\n\n        string numStr = to_string(num);\n        if (numStr[0] == \'-\') {\n            sort(numStr.begin() + 1, numStr.end(), [](const char& c1, const char& c2) {\n                return c1 > c2;\n            });\n        }\n        else {\n            sort(numStr.begin(), numStr.end());\n\n            int firstNonZeroIdx = 0;\n            while (numStr[firstNonZeroIdx] == \'0\') ++firstNonZeroIdx;\n\n            swap(numStr[0], numStr[firstNonZeroIdx]);\n        }\n\n        return stoll(numStr);\n    }\n};\n```
[]