Skip to content

Instantly share code, notes, and snippets.

@codertcet111
Created March 29, 2024 15:03
Show Gist options
  • Save codertcet111/e9b92dc837e765229fcba33028fafa6e to your computer and use it in GitHub Desktop.
Save codertcet111/e9b92dc837e765229fcba33028fafa6e to your computer and use it in GitHub Desktop.
Leetcode 7 reverse integer atoi
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