Swift generic method


  • 0
    I
    func searchMatrix<T:Comparable>(a:[[T]],searchedItem:T) ->Bool
    {
        if(a.count == 0 || a[0].count == 0)
        {
            return false
        }
        var row = 0
        var col = a[0].count - 1
        while (row < a.count && col >= 0)
        {
            if(searchedItem < a[row][col])
            {
                col--
            }
            else if(searchedItem > a[row][col])
            {
                row++
            }
            else if(searchedItem == a[row][col]){
                return true
            }
        }
        return false
    }
    
    

Log in to reply
 

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