PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php class Solution { /** * @param String $s * @return Integer */ function myAtoi($s) { $min = pow(-2,31); $max = pow(2,31) - 1; $chars = strlen($s); $newStr = ''; for($index = 0; $index < $chars; $index++){ $currentChar=$s[$index]; $canConcat = is_numeric($currentChar) || $currentChar == '-'; $isAlphabetic = preg_match("/^[a-zA-Z]$/", $currentChar); if($index == 0 && $canConcat == false && $isAlphabetic){ $newStr = 0; break; } if($canConcat){ $newStr .= $currentChar; } } $number = intval($newStr); if($number <= $min){ // var_dump('hello'); return $min; } else if($number > $max){ return $max; }else{ return $number; } } } echo (new Solution())->myAtoi("words and 987");
Show:  
Copy Clear