A recursive algorithm to find two integers in an array that sums to a given integer

后端 未结 8 1202
Happy的楠姐
Happy的楠姐 2021-01-16 19:36

I need an algorithm to determine if an array contains two elements that sum to a given integer.

The array is sorted.

The algorithm should be recursiv

8条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-16 19:47

    I think hash is ok, for example, 1,3,7,9,12,14,33...

    if we want sum=21, we hash the numbers into a hash table, So, O(n).

    we iterator them, when we get 7, we let 21-7=14, so we hash 14, we can find it. so 7+14=21,

    we got it!

提交回复
热议问题