一中依靠int和long long 自身特性判断溢出的写法
反转数字
http://www.nowcoder.com/questionTerminal/1a3de8b83d12437aa05694b90e02f47a
class Solution {
public:
/**
*
* @param x int整型
* @return int整型
*/
int reverse(int x) {
// write code here
if( 0==x )
{
return 0;
}
char solve[15];
sprintf(solve,"%d",x);
long long sum=0;
int temp=0;
int tag=1;//权重
bool flag=false;//不是负数
int len=strlen( solve );
int left=0,right=len-1;
while( left<right )
{
swap(solve[left], solve[right]);
++left;
--right;
}
while( len-- )
{
if( '-'==solve[len] )
{
flag=true;
continue;
}
int num=tag*(solve[len]-'0');
tag*=10;
sum+=(long long)num;
temp+=num;
}
if( temp!=sum )
{
return 0;
}
else
{
if( flag )
{
return -1*temp;
}
else
{
return temp;
}
}
}
}; 