题解 | #反转数字#
反转数字
https://www.nowcoder.com/practice/1a3de8b83d12437aa05694b90e02f47a
注意反转后的结果可能会溢出,因此需要将num的数据类型修改为long
#include <climits> #include <cmath> class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ int reverse(int x) { // write code here long num = 0; int temp = x; while (temp != 0) { num = num * 10 + temp % 10; temp /= 10; } if(num > INT_MAX || num < INT_MIN) return 0; return num; } };