当前位置:网站首页>Sword finger offer 04: find in 2D array

Sword finger offer 04: find in 2D array

2022-06-11 05:46:00 swindler.

Two dimensional array search

Their thinking : Find elements in an ordered array , You should start with the vertex , This topic starts from the vertex in the lower left corner , In turn target Compare with the elements in an ordered array

class Solution {
    
    func findNumberIn2DArray(_ matrix: [[Int]], _ target: Int) -> Bool {
    
    var row = matrix.count - 1 
    var col = 0
    while row >= 0 && col <= matrix[0].count - 1 {
    
        if matrix[row][col]>target{
    
            row -= 1
        }
        else if matrix[row][col]<target{
    
            col += 1
        }
        else{
    
            return true
        }
    }
     return false  
    }
}
原网站

版权声明
本文为[swindler.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020534563425.html