LeetCode 算法习题 第三周

来源:互联网 发布:1025实验室 知乎 编辑:程序博客网 时间:2024/06/05 15:20

Reverse Integer

Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321

题目大意

将一个整数进行位数调换,使得最高位成为个位,次高位成为十位,……
比如123 变成321

我的解答

class Solution {public:    int reverse(int x) {        int ans = 0;        while (x) {            int temp = ans * 10 + x % 10;            if (temp / 10 != ans)                return 0;            ans = temp;            x /= 10;        }        return ans;    }};
原创粉丝点击