c*******t 发帖数: 1095 | 1 O(n^2)下面这段code有反例么? 没想出来反例
谢
for(each ele in the sorted array)
{
ele = arr[i] - k;
let front be the pointer to the front of the array;
let rear be the pointer to the rear element of the array.;
// till front is not greater than rear.
while(front <= rear)
{
if(*front *rear == ele)
{
print "Found triplet "<<*front<<","<<*rear<<","<
break;
}
else
{
// sum is > ele, so we need to decrease the sum by decrementing
rear pointer.
if((*front *rear) > ele)
decrement rear pointer.
// sum is < ele, so we need to increase the sum by incrementing
the front pointer.
else
increment front pointer.
}
} |
p*****2 发帖数: 21240 | |
l*****a 发帖数: 14598 | 3 问别人好歹拿点readable的code吧?
what does below 2 statements mean?
ele = arr[i] - k;
if(*front *rear == ele)
【在 c*******t 的大作中提到】 : O(n^2)下面这段code有反例么? 没想出来反例 : 谢 : for(each ele in the sorted array) : { : ele = arr[i] - k; : let front be the pointer to the front of the array; : let rear be the pointer to the rear element of the array.; : // till front is not greater than rear. : while(front <= rear) : {
|
b*****n 发帖数: 482 | 4 应该是对的. 写了个code过了OJ
int threeSumClosest(vector &num, int target) {
sort(num.begin(), num.end());
int bestSum;
int diff = INT_MAX;
for(int i=0;i
int l=i+1, r=num.size()-1;
while(l
int sum = num.at(i) + num.at(l) + num.at(r);
// found perfect sum, return immediately
if(sum == target)
return sum;
// update diff and bestSum
if(abs(sum-target) < diff) {
diff = abs(sum-target);
bestSum = sum;
}
// pick larger element by increasing l
// or smaller element by decreasing r
if(sum < target) l++;
else r--;
}
}
return bestSum;
}
【在 c*******t 的大作中提到】 : O(n^2)下面这段code有反例么? 没想出来反例 : 谢 : for(each ele in the sorted array) : { : ele = arr[i] - k; : let front be the pointer to the front of the array; : let rear be the pointer to the rear element of the array.; : // till front is not greater than rear. : while(front <= rear) : {
|
c*******t 发帖数: 1095 | 5 不好意思,用iphone复制的,结果“+” 没有显示出来。。。。
【在 l*****a 的大作中提到】 : 问别人好歹拿点readable的code吧? : what does below 2 statements mean? : ele = arr[i] - k; : if(*front *rear == ele)
|