public bool SearchMatrix(int[,] matrix, int target)
{
int m = matrix.GetLength(0);
int n = matrix.GetLength(1);
int l = 0, r = m*n 1, mid = 0, x = 0, y = 0;
while( l <= r )
{
mid = (l+r)/2;
y = mid / n;
x = mid  y * n;
if( matrix[y,x] == target)
{
return true;
}
else if( matrix[y,x] > target )
{
r = mid  1;
}
else
{
l = mid + 1;
}
}
return false;
}
C# Accepted Solution


A same solution
public bool SearchMatrix(int[,] matrix, int target) { int m = matrix.GetLength(0), n = matrix.GetLength(1), left = 0, right = m * n  1; while(left <= right){ int mid = (left + right) / 2, y = mid % n, x = (mid  y) / n; if(matrix[x, y] == target) return true; else if (matrix[x, y] < target) left = mid + 1; else right = mid  1; } return false; }