16ms c++ solution


  • 4
    Z
    class Solution {
    public:
        bool searchMatrix(vector<vector<int>>& matrix, int target) {
            int size = matrix.size();
            if(size == 0) return false;
            int colsize = matrix[0].size();
            if(colsize == 0) return false;
            int elmcount = size*colsize;
            int begin = 0,end = elmcount-1;
            int mid,t;
            while(begin<=end)
            {
                mid = (begin+end)/2;
                if((t = matrix[mid/colsize][mid%colsize]) == target)
                return true;
                else if(t<target)
                {
                    begin = mid+1;
                }
                else
                end = mid-1;
            }
            return false;
        }
    };

Log in to reply
 

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