[google面试题]describe an algorithm to determine whether two of the numbers add up to x.
Good Answer:Sort the array.Then,keep track of two pointers in the array,one atthe beginning and one at the end.Whenever the sum of the current two integers isless than x,move the first pointer forwards,and whenever the sum is greater than x,move these condpointer backwards.If you cannot find two numbers that add to xbefore one of the pointers meet,then there is no pair of integers that sum to x.
页:
[1]