Simple C# O(mlogn) solution


  • 0
    N
    public class Solution {
         public bool SearchMatrix(int[,] matrix, int target)
            {
                int rowLen = matrix.GetLength(0);
                int colLen = matrix.GetLength(1);
                for (int row = 0; row < rowLen; row++)
                {
                    if (BinarySearch(matrix, target, 0, colLen-1, row) >= 0)
                    {
                        return true;
                    }
                }
    
                return false;
            }
    
            public int BinarySearch(int[,] matrix, int target, int start, int end, int rowNum)
            {
                if (start > end)
                {
                    return -1;
                }
                else
                {
                    int mid = (start + end) / 2;
                    if (matrix[rowNum, mid] > target)
                    {
                        return BinarySearch(matrix, target, start, mid - 1, rowNum);
                    }
                    else if (matrix[rowNum, mid] < target)
                    {
                        return BinarySearch(matrix, target, mid + 1, end, rowNum);
                    }
                    else
                    {
                        return mid;
                    }
                }
            }
    }

Log in to reply
 

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