问题描述:
反转整数,例子:
输入:x = 123, return 321; x = -123, return -321
方法:
class Solution { public: int reverse(int x) { int newX = 0; int sign = 1; int tempX = 0; if(x == 0) newX = 0; // 处理负数 if(x < 0){ sign = -1; x = sign * x; } while(x != 0){ tempX = x % 10; newX = newX * 10 + tempX; x /= 10; } return sign * newX; } };
时间: 2024-10-12 18:28:04