Untitled

mail@pastecode.io avatar
unknown
plain_text
22 days ago
775 B
2
Indexable
Never
class Solution {
public:
    long long int power(int base, int exponent) {
        long long int result = 1;
        for (int i = 0; i < exponent; i++) {
            result *= base;
        }
        return result;
    }
    int reverse(int x) {
        int sum = 0;
        int factor = -1;
        int temp = abs(x);
        int multiply = x < 0 ? -1 : 1;
        long long int result = 0;

        while (temp > 0) {
            temp = temp / 10;
            factor++;
        }

        x = abs(x);
        while (x > 0) {
            result += (x % 10) * (power(10, factor));
            factor--;
            x /= 10;
        }

        result = result * multiply;
        if (result > INT_MAX || result < INT_MIN)
            return 0;

        return result;
    }
};
Leave a Comment