leetcode Reverse Integer

来源:互联网 发布:剑网三mac可以玩吗 编辑:程序博客网 时间:2024/06/05 10:11

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;

}

0 0
原创粉丝点击