Created
March 29, 2024 15:02
-
-
Save codertcet111/da76c0ac54768bccea4d09746e2961ef to your computer and use it in GitHub Desktop.
Leetcode 8: String to integer atoi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Leetcode 8: String to integer atoi | |
# @param {String} s | |
# @return {Integer} | |
def my_atoi(s) | |
return nil if s == nil | |
r_n = s.gsub(/[^0-9-]/, "").to_i | |
max_n = (1 << 31) - 1 | |
r_n > max_n || r_n < (max_n * -1) ? 0 : r_n | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment