C# solution: start from bottom-left


  • 0
    B
    public class Solution 
    {
        public bool SearchMatrix(int[,] matrix, int target) 
    	{
            var row = matrix.GetLength(0);
    		var col = matrix.GetLength(1);
    
    		var curX = row - 1;
    		var curY = 0;
    
    		while(curX >= 0 && curY < col)
    		{
    			var matrixValue = matrix[curX, curY];
    			if (matrixValue == target) return true;
    			else if (matrixValue < target)
    			{
    				curY++;
    			}
    			else if (matrixValue > target)
    			{
    				curX--;
    			}
    		}
    
    		return false;
        }
    }
    

Log in to reply
 

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