classSolution { public: intreverse(int x){ longlong res = 0; while (x!=0){ res = res*10 + x%10; x /= 10; } if (res>pow(2,31)-1 || res<(pow(-2,31))){return0;} return res; } };
执行用时: 4 ms , 在所有 Swift 提交中击败了 95.41% 的用户 内存消耗:13.7 MB , 在所有 Swift 提交中击败了 25.63% 的用户
1 2 3 4 5 6 7 8 9 10 11 12 13 14
classSolution{ funcreverse(_x: Int) -> Int { var value = x var res =0 while value !=0 { res = res*10+ value%10 if res >Int32.max || res <Int32.min { return0 } value /=10 } return res } }