C # wins the offer problem-solving course (a)

This article is used to record their problem-solving process, if there are problems, please contact deleted.

Topic: Finding from a two-dimensional array that contains integer need to find a consistent line of the two-dimensional array, each row from left to right in order of ascending sort each column are sorted in ascending order from top to bottom.

Thinking: This is the first line of each column is incremented each two-dimensional array, if you start looking at the bottom left (such as a two-dimensional array at a [0] [Length-1]) If the number is greater than the lower left corner on the to the right, to the left is small.



code show as below:

class Solution

{

    public bool Find(int target, int[][] array)

    {

        // write code here

        int row = 0;

        int col = array[0].Length-1;

        while(col>=0&&row<=array[0].Length-1)

        {

            if (array[row][col] == target)

              return true;

            else if(target>array[row][col])

                row++;

            else

                col--;

        }

        return false;

    }

}

Reproduced in: https: //www.jianshu.com/p/3fe8546e4f30

Guess you like

Origin blog.csdn.net/weixin_34289744/article/details/91085317