3、二维数组中的查找

来源:互联网 发布:python 伪装成浏览器 编辑:程序博客网 时间:2024/06/05 20:30

数组的条件:从上至下和从左至右都是递增的

class Program    {        static void Main(string[] args)        {            int[,] nums = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 }, { 6, 8, 11, 15 } };            Console.WriteLine(Find(nums, nums.GetLength(0), nums.GetLength(1), 8));            Console.ReadKey();        }        static bool Find(int[,] nums, int rows, int colomns, int target)        {            bool isFind = false;            if (rows > 0 && colomns > 0)            {                // row,column 就代表下标了                int row = 0;                int column = colomns - 1;                while (row < rows && column < colomns)                {                    if (nums[row, column] == target)                    {                        isFind = true;                        return isFind;                    }                    else if (nums[row, column] < target)                    {                        ++row;                    }                    else if (nums[row, column] > target)                    {                        --column;                    }                }            }            return false;        }




原创粉丝点击