Golang 0ms AC


  • 0
    R
    import ( "strings"
            "strconv" )
    
    func compareVersion(version1 string, version2 string) int {
    
        ver1Arr := strings.Split(version1, ".")
        ver2Arr := strings.Split(version2, ".")
        
        length := len(ver1Arr)
        if length < len(ver2Arr) {
            length = len(ver2Arr)
        }
        
        for i:=0; i < length; i++ {
            v1 := 0
            if i < len(ver1Arr) {
                v1, _ = strconv.Atoi(ver1Arr[i])
            }
            v2 := 0
            if i < len(ver2Arr) {
                v2, _ = strconv.Atoi(ver2Arr[i])
            }
            cp := 0
            if v1 > v2 {
                cp = 1
            } else if v2 > v1 {
                cp = -1
            }
            if cp != 0 {
                return cp
            }
        }
        return 0
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.