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

反转数字字符串 #570

Open
Twlig opened this issue Apr 13, 2022 · 2 comments
Open

反转数字字符串 #570

Twlig opened this issue Apr 13, 2022 · 2 comments

Comments

@Twlig
Copy link

Twlig commented Apr 13, 2022

No description provided.

@Twlig Twlig closed this as completed Apr 13, 2022
@Twlig Twlig reopened this Apr 13, 2022
@TheFirstSunday
Copy link

function reverseNum(num) {
      if(!num) return num; 
      if(num < 0) Number('-' + num.toString().slice(1).reverse());
      return Number(num.toString().reverse());
}

@TheFirstSunday
Copy link

TheFirstSunday commented Apr 18, 2022

const max = Math.pow(2, 31) - 1;
const min = -Math.pow(2, 31);

const reverseNum = function(x) {

    var y = 0;
    while(x !== 0) {
        y = 10 * y + x % 10;
        x = ~~(x/10);
    }
    if (y > max) return 0;
    if (y < min) return 0;
    return y;
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants