Reverse Integer
My SubmissionsTotal Accepted: 112034 Total Submissions: 477710 Difficulty: EasyReverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
Subscribe to see which companies asked this question
Hide Tags Math Show Similar Problems//思路想清楚:要先知道數的正負 //先一位一位的個位取出來(他是新數的最高位,循環一次*10),取了之後再將這個數縮小10倍(取整)重復如此操作 //題目需要溢出處理 class Solution { public: int reverse(int x) { int sgn=-1; if(x<0) x=x*sgn; else sgn=1; long long ans=0; while(x) { ans*=10; ans+=x%10; x/=10; } if(sgn==-1) ans*=-1; //溢出判斷 if(ans>INT_MAX || ans