Golang solution


  • 0
    D

    Golang

    func findRestaurant(list1 []string, list2 []string) []string {
        var result []string
        map1 := make(map[string]int)
        map2 := make(map[string]int)
        min := 9223372036854775807
        
        for i := 0; i < len(list1); i++ {
            map1[list1[i]] = i
        }
        
        for j := 0; j < len(list2); j++ {
            map2[list2[j]] = j
        }
        
        
        for i := 0; i < len(list1); i ++ {
            _, ok := map2[list1[i]]
            if ok {
                sum := map1[list1[i]] + map2[list1[i]]
                if sum < min {
                    min = sum
                    result = append(result, list1[i])
                } else if (sum == min) {
                    result = append(result, list1[i])
                }
            }
        }
        
        return result
    }
    
    

Log in to reply
 

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