if we know memory layout, 0ms....


  • 0
    0
    class Solution {
    public:
        string toHex(int num) {
            static string t = "0123456789abcdef";
            string r;
            struct {int a; int b;} v = {num, 0};
            long long n = *reinterpret_cast<long long*>(&v);
            while (n > 0) {
                r.push_back(t[n & 0xF]);
                n >>= 4;
            }
            reverse(r.begin(), r.end());
            if (r.empty()) r = "0";
            return r;
        }
    };

  • 0

    The general statistic says 68.15% of everybody's C++ submissions achieved 0ms. I just submitted your solution five times, got 3ms, 3ms, 0ms, 0ms, 3ms.

    Rather underwhelming, after that title.


Log in to reply
 

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