
g0001_0100.s0008_string_to_integer_atoi.Solution.ex Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
# #Medium #Top_Interview_Questions #String #2024_07_31_Time_352_ms_(55.56%)_Space_72.2_MB_(77.78%)
defmodule Solution do
@spec my_atoi(s :: String.t) :: integer
def my_atoi(s) do
result = s
|> String.trim
|> String.split
|> List.first
if result != nil and Integer.parse(result) != :error do
{num,_} = Integer.parse(result)
cond do
num < -2 ** 31 -> -2 ** 31
num > 2 ** 31 - 1 -> 2 ** 31 - 1
true -> num
end
else
0
end
end
end
© 2015 - 2025 Weber Informatics LLC | Privacy Policy