LeetCode 66 Plus One

题意:

给出高精度数,输出它+1的结果


思路:

从个位开始加,如果某个时刻进位已经没有了,那么没有必要再加下去了。如果一直进位,最后记得添加一个最高位。


代码:

class Solution {
public:
    int mySqrt(int x) {
        if (x == 0 || x == 1) {
            return x;
        }
        int ans = 1;
        int i = 2;
        while (x != 1) {
            int i2 = i * i;
            while (x % i2 == 0) {
                x /= i2;
                ans *= i;
            }
            ++i;
        }
        return ans;
    }
};




你可能感兴趣的:(LeetCode,杂题,LeetCode,杂题)