How do I search for a number in a 2d array sorted left to right and top to bottom?

后端 未结 20 950
暖寄归人
暖寄归人 2020-11-22 13:03

I was recently given this interview question and I\'m curious what a good solution to it would be.

Say I\'m given a 2d array where all the numbers i

20条回答
  •  萌比男神i
    2020-11-22 13:16

    Binary search would be the best approach, imo. Starting at 1/2 x, 1/2 y will cut it in half. IE a 5x5 square would be something like x == 2 / y == 3 . I rounded one value down and one value up to better zone in on the direction of the targeted value.

    For clarity the next iteration would give you something like x == 1 / y == 2 OR x == 3 / y == 5

提交回复
热议问题