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

后端 未结 20 725
暖寄归人
暖寄归人 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条回答
  • 2020-11-22 13:31

    A. Do a binary search on those lines where the target number might be on.

    B. Make it a graph : Look for the number by taking always the smallest unvisited neighbour node and backtracking when a too big number is found

    0 讨论(0)
  • 2020-11-22 13:34

    I suggest, store all characters in a 2D list. then find index of required element if it exists in list.

    If not present print appropriate message else print row and column as:

    row = (index/total_columns) and column = (index%total_columns -1)

    This will incur only the binary search time in a list.

    Please suggest any corrections. :)

    0 讨论(0)
提交回复
热议问题