leetcode Reverse Integer

来源:互联网 发布:dbc2000数据库64位 编辑:程序博客网 时间:2024/05/16 01:00

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321


class Solution {public:    int reverse(int x) {        bool flag = x < 0 ?0:1;        ostringstream os;        os<<x;        string str;        str = os.str();        if (!flag) str = str.substr(1);        std::reverse(str.begin(),str.end());        stringstream stream;        stream << str;        int n;        stream >>n;        if (!flag) n = -n;        if (n == 2147483647 || n == -2147483647){            return 0;        }        return (int)n;    }};


0 0