题解 | #矩阵元素查找#

矩阵元素查找

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

class Solution {
public:
    vector<int> findElement(vector<vector<int> > mat, int n, int m, int x) {
        // write code here
        vector<int> A;
        if(mat.size()<=0||mat[0].size()<=0)
        return A;
        int i=n-1,j=0;//从左下角开始查找x
        while(i>=0&&j<m){
            if(mat[i][j]==x){
                A.push_back(i);
                A.push_back(j);
                return A;
            }
            else if(x>mat[i][j]){
                j++;
            }
            else i--;
        }
        return A;
    }
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
02-12 18:14
RT,这周五就是情人节了,前女友给我发了消息,我该不该回?
Yoswell:原则上来说让她滚,但是本着工作很累下班想吃瓜的心态,我觉得你可以回一下
点赞 评论 收藏
分享
牛客鼠:校友你这简历基本无敌了,春招刷刷题去冲大厂
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务