您的位置:首页 > 其它

leetcode Reverse Integer

2015-05-25 23:47 429 查看

Reverse Integer

 TotalAccepted: 73083 TotalSubmissions: 281068My
Submissions

Question Solution 

Reverse digits of an integer.

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

click to show spoilers.

 

 本题就是将int翻转下,有正负。唯一的坑在于int翻转之后可能超过int的范围,所以要用double来定义,同时要判断是否超出范围,超出直接返回0即可。

 

Ac代码源码(有main()):

#include <iostream>

using namespace std;

int f(int x)

{

      intmax =0x7fffffff;

      intmin= 0x80000000;

      doubley=0;

      while(x)

      {

           y=y*10+x%10;

           x/=10;

      }

      if(y>max||y<min)

      y=0;

      returny;

}

int main()

{

      intn;

      while(cin>>n)

      cout<<f(n)<<endl;

      return0;

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