Skip to content

Instantly share code, notes, and snippets.

@cfc1020
Last active June 3, 2020 18:51
Show Gist options
  • Save cfc1020/74ecba52dec4433f81f388442720a860 to your computer and use it in GitHub Desktop.
Save cfc1020/74ecba52dec4433f81f388442720a860 to your computer and use it in GitHub Desktop.
Compare Versions
class MySolution
# @param a : string
# @param b : string
# @returns an integer
def compare_versions(a, b)
aa = a.split('.')
bb = b.split('.')
i = 0
loop do
if i < aa.length && i < bb.length
if aa[i].to_i > bb[i].to_i
return 1
elsif aa[i].to_i < bb[i].to_i
return -1
end
else
if i < aa.length && aa[i].to_i.nonzero?
return 1
elsif i < bb.length && bb[i].to_i.nonzero?
return -1
else
return 0
end
end
i += 1
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment