Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

7. 整数反转 #34

Open
zpc7 opened this issue Aug 17, 2022 · 0 comments
Open

7. 整数反转 #34

zpc7 opened this issue Aug 17, 2022 · 0 comments
Labels
中等 LeetCode 难度定级 数学

Comments

@zpc7
Copy link
Owner

zpc7 commented Aug 17, 2022

7. 整数反转

function reverse(x: number): number {
    let res = 0;
    while (x !== 0) {
        // 整数部分
        const digit = x % 10;
        // 每一次都在上一次的基础上乘10
        res = res * 10 + digit;
        // 除数取整
        x = Math.trunc(x / 10);

        // 超出范围的判断
        if (res < Math.pow(-2, 31) || res > Math.pow(2, 31) - 1) return 0;
    }

    return res;
};
@zpc7 zpc7 added 中等 LeetCode 难度定级 数学 labels Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
中等 LeetCode 难度定级 数学
Projects
None yet
Development

No branches or pull requests

1 participant