My Simple C++ Solution


  • 0
    S

    class Solution {
    public:
    int reverse(int x) {
    int sum = 0;
    if(!(x%10) && !(x/10)) {
    return 0;
    }

        while(x){
            int tsum = sum*10+(x%10);
            if (tsum / 10 != sum)
    		    return 0;
            sum = tsum;
            x /= 10;
        }
        return sum;
    }
    

    };


  • 0
    T
     public int ReverseIntigerVer2 (int x)
            {
                int sum = 0;
                bool isPosetive = true;
                if (x<0)
                {
                    isPosetive = false;
                    x = Math.Abs(x);
                }
                if((x%10)==0 && (x/10)==0) { return 0; }
                    while (x>0)
                    {
                        int tsum = sum * 10 + (x % 10);
                        if (tsum / 10 != sum)
                            return 0;
                        sum = tsum;
                        x /= 10;
                    }
                    if (!isPosetive) sum *= -1;
                    return sum;
                 }

Log in to reply
 

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