checking if 2 numbers of array add up to I

后端 未结 15 2667
日久生厌
日久生厌 2020-12-15 01:13

I saw a interview question as follows: Give an unsorted array of integers A and and an integer I, find out if any two members of A add up to I.

any clues?

ti

15条回答
  •  孤城傲影
    2020-12-15 01:45

    for each ele in the array
      if (sum - ele) is hashed and hashed value is not equal to index of ele
        print ele, sum-ele
      end-if
      Hash ele as key and index as value
    end-for
    

提交回复
热议问题