LCR 179. 查找总价格为目标值的两个商品 - 力扣(LeetCode)
思路:
代码:
public int[] twoSum(int[] price, int target) {
int left = 0;
int right = price.length - 1;
while(left < right){
//每一次都重新计算和
int sum = price[left] + price[right];
if(sum < target){
left++;
}else if(sum > target){
right--;
}else{
return new int[] {price[left],price[right]};
}
}
return new int[] {0};
}