Golang divide and conquer


  • 0
    R
    func longestCommonPrefix(strs []string) string {
        if strs == nil || len(strs) == 0 {
            return ""
        }
        l:=len(strs)
        if l == 1 {
            return strs[0]
        }
        strs1 := make([]string, l / 2)
        strs2 := make([]string, l - (l/2))
        for i:= 0; i < l/2; i++ {
            strs1[i] = strs[i]
        }
        for i:=l/2; i < l; i++ {
            strs2[i-l/2] = strs[i]
        }
        s1:=longestCommonPrefix(strs1)
        s2:=longestCommonPrefix(strs2)
        i:=0
        for i<len(s1) && i < len(s2) && s1[i] == s2[i]{
            i++
        }
        return s1[0:i]
    }
    

Log in to reply
 

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