2025年5月25日 星期日

7. Reverse Integer

 7. Reverse Integer

難度: Medium
類型: Math
CPP程式下載: 7.cpp

前情題要:
把數字做十進位的反轉









思考方式:

其實這題很簡單, 反而難的地方在判斷邊界, 如果反轉後數字超過 int32 的範圍, 要回傳 0。我程式裡的 a10 跟 a01, 就是為了判斷這個而生。


複雜度思考:

因為 0x7FFFFFFF = 2,147,483,647。十進位共10位數

Time Complexity: O( 10 ) 
Space Complexity: O( 1 )

結果:

Runtime: 0 ms, Beats: 100%

Memory: 8.47 MB, Beats: 82.10%