发表于 2013-7-30 16:48:01

[google面试题]describe
 an
 algorithm
 to
 determine
 whether 
two
 of 
the 
numbers
 add 
up
 to
 x.

邢城 发表于 2013-7-30 17:14:46

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]
查看完整版本: [google面试题]describe
 an
 algorithm
 to
 determine
 whether 
two
 of 
the 
numbers
 add 
up
 to
 x.