Javascript "seriously?" code


  • 12
    D
    var myAtoi = function(str) {
        return Math.max(Math.min(parseInt(str) || 0, 2147483647), -2147483648)
    };
    

    apparently the included parseInt() function does most of the requirements already


  • 0
    B

    @dajavax
    What am I missing here?
    Submission Result: Wrong Answer
    Input: "2147483648"
    Output: 2147483648
    Expected: 2147483647
    do they not allow for 31 bit numbers?


  • 0
    D

    @BeardOfDan
    An "int" is usually 32 bits in length. At least it always is in Java and most of the time in C. Anything bigger is either a long, a long long, or something else.

    A 32 bit signed number has a range of −2,147,483,648 to +2,147,483,647.


  • 0
    Z

    Hi I just wonder can we use parseInt() during an interview?
    if not do we need to write parseInt() by ourself?


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.