Last active
June 3, 2020 18:51
-
-
Save cfc1020/74ecba52dec4433f81f388442720a860 to your computer and use it in GitHub Desktop.
Compare Versions
This file contains hidden or 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
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