日期 | 是否一次通过 | comment |
---|---|---|
2019-02-10 20:20 | N |
题目:返回乘积最小的一组two sum
思路:
- two sum问题;
- 乘积最小,则二者相差最大,只好用two point方法了。
1. two point
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> FindNumbersWithSum(int[] array,int sum) {
ArrayList<Integer> result = new ArrayList<>();
if(array == null || array.length == 0) {
return result;
}
int sta = 0;
int end = array.length - 1;
while(sta < end) {
if(array[sta] == sum-array[end]) {
result.add(array[sta]);
result.add(array[end]);
break;
} else if(array[sta] > sum-array[end]) {
end --;
} else {
sta ++;
}
}
return result;
}
}
网友评论