-
Notifications
You must be signed in to change notification settings - Fork 0
/
剑指0ffer_和为s 的俩个乘积最小的数.txt
46 lines (26 loc) · 1.11 KB
/
剑指0ffer_和为s 的俩个乘积最小的数.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class Solution {
public:
vector<int> FindNumbersWithSum(vector<int> array,int sum) {
if (array.size()<2) return vector<int>();
int mini = INT_MAX, a0 = 0 , b0 = 0,product = 0;
int left=0;
int right=array.size()-1;
bool flag=false;
while (left<right){
if (array[left]+array[right]>sum) right--;
else if (array[left]+array[right]<sum) left++; //עŇâelse if
else{
flag=true;
product=array[left]*array[right];
if(product<mini){
a0=array[left];
b0=array[right];
mini=product;
}
left++; //źĚĐřɨ
}
}
if (flag) return vector<int>{a0,b0};
else return vector<int>();
}
};