Leetcode 7. Reverse Integer

来源:互联网 发布:没文凭找工作 知乎 编辑:程序博客网 时间:2024/06/11 14:45

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

class Solution {public:    int reverse(int x) {        string s=to_string(x);        for(int i=0,j=s.size()-1;i<j;++i,--j){            swap(s[i],s[j]);        }        if(x<0)            return -atoi(s.c_str());        else            return atoi(s.c_str());    }};
0 0