双指针解法(Java)
和为S的两个数字
http://www.nowcoder.com/questionTerminal/390da4f7a00f44bea7c2f3d19491311b
public ArrayList<Integer> FindNumbersWithSum(int [] array,int sum) { if(array == null || array.length<=1)return new ArrayList<>(); int left = 0,right = array.length - 1; ArrayList<Integer> list = new ArrayList<>(); while(left<right){ int temp = array[left] + array[right]; if(temp > sum){ right--; }else if(temp <sum){ left++; }else{ list.add(array[left]); list.add(array[right]); break; } } return list; }