Created
March 29, 2024 15:03
-
-
Save codertcet111/e9b92dc837e765229fcba33028fafa6e to your computer and use it in GitHub Desktop.
Leetcode 7 reverse 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 7 reverse integer atoi | |
# @param {Integer} x | |
# @return {Integer} | |
def reverse(x) | |
o_num = if x < 0 | |
"-#{x.to_s.split("")[1..-1].join("").reverse}".to_i | |
else | |
x.to_s.reverse.to_i | |
end | |
max_l = (1 << 31) - 1 | |
o_num > max_l || o_num < (-1 * max_l) ? 0 : o_num | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment