您的位置:首页 > 其它

LeetCode_Reverse Integer

2013-07-26 11:23 281 查看
Reverse digits of an integer.

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


class Solution {
public:
int reverse(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x == 0) return x;
int flag = 1;

if(x < 0)
{
x*= -1;
flag = -1;
}
vector<int> result;
while(x)
{
int temp = x %10;
result.push_back(temp);
x/=10;
}
int num = 0;
for(int i = 0; i< result.size(); i++)
{
num +=result[i];
num*=10;
}

return flag*num/10 ;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: