I solved the problem by using regular expression in Javascript.Only less 20 lines


  • -1
    S

    var myAtoi = function(str) { let minInteger = -2147483648,maxInteger = 2147483648; str = str.replace(/^\s*|\s*$/g,""); var arr = str.split(" "); for(var i = 1;i < arr[0].length;i++){ if(isNaN(arr[0].charAt(i))){ arr[0] = arr[0].slice(0,i); break; } } if(parseFloat(arr[0])){ arr[0] = parseFloat(arr[0]); if(arr[0] < minInteger) return minInteger; else if(arr[0] > maxInteger) return maxInteger; else return parseFloat(arr[0]); } return 0; };


Log in to reply
 

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