這道題其實就是leetcode上的整數翻轉問題,應該是第七道題吧
簡單的數字變換取餘做除法就能做出來,但是要注意的是他提到int,就要考慮溢出的問題,說出溢出單獨判斷應該就能通過
class Solution {
public:
int reverse(int x)
{
const int int_max=0x7fffffff;
const int int_min=0x80000000;
long long anwser=0;
while(x!=0)
{
anwser=anwser*10+(x%10);
x/=10;
}
if(anwser<int_min || anwser>int_max)
{
anwser=0;
}
return anwser;
}
};
到這一步還沒有完,如果想要一個亮點的話,再給他一個答案就好了
return x / Math.Abs(x) * Int.Parse(String.Reverse(Math.Abs(x).ToString()));
通過轉換成字符串來操作