//reverse interger #include <iostream> using namespace std; int reverse(int x){ int temp = abs(x),t,goal=0; if (x>0){t=1;}else{t=-1;} const int max = 0x7fffffff; //设置最大最小数,解决溢出问题 const int min = 0x80000000; while(temp%10!=0||temp!=0){ if(goal>(max/10)||goal<(min/10)){ return 0;} //goal*10则会溢出 goal = goal*10 + temp%10; temp = temp/10; } goal = goal * t; return goal; } int main(){ int a = 1534236469; cout<<reverse(a)<<endl; return 0; }
leetcode reverse
最新推荐文章于 2022-11-23 09:29:19 发布