题解 | #矩阵元素查找#(有序矩阵查找,按行列来查)

矩阵元素查找

http://www.nowcoder.com/practice/3afe6fabdb2c46ed98f06cfd9a20f2ce

/*


*/
class Solution {
public:
    vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
        vector<int> ans;
        if(n == 0 || m == 0) return ans;
        int row = 0, col = m-1;
        while( row < n && col >= 0){
            if(mat[row][col] < x) row++;
            else if(mat[row][col] > x) col--;
            else {
                ans.push_back(row);
                ans.push_back(col);
                return ans;
            }
        }
        return ans;
    }
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务