Swift 3 O(n^2) Solution


  • 0
    D
    class Solution {
        func findRestaurant(_ list1: [String], _ list2: [String]) -> [String] {
            var leastIndex:(Int,Int) = (list1.count-1, list2.count-1)
            var answerArr:Array<String> = []
            var i = 0
        
            for restaurant1 in list1 {
                var j = 0
                for restaurant2 in list2{
                    if(restaurant1 == restaurant2) {
                        if(i+j<leastIndex.0+leastIndex.1){
                            leastIndex.0=i
                            leastIndex.1=j
                            answerArr.removeAll()
                            answerArr.append(restaurant1)
                        }else if i+j==leastIndex.0+leastIndex.1 {
                            answerArr.append(restaurant1)
                        }
                        break
                    }
                    j += 1
                }
                i += 1
            }
            return answerArr
        }
    }

Log in to reply
 

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