由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问求array中3个数和最接近k的解法
相关主题
以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)Extension problem of finding intersection of two sorted array
A simple google interview questionLRU Cache class:有没有面试可用的精简一些的Sample Code
[a9面经] print x,y,z攒人品之facebook电面面经
CS algorithm question请问一个面试题
一FG家常见题一道google 经典题
请教一个问题,发两个包子。find sum of three number in array
careercup上这道题我竟然没看懂请教一道L的题
贡献两道google面试题这道题咋做?
相关话题的讨论汇总
话题: ele话题: rear话题: front话题: sum话题: pointer
进入JobHunting版参与讨论
1 (共1页)
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
2
就是3sum吧。
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)

1 (共1页)
进入JobHunting版参与讨论
相关主题
这道题咋做?一FG家常见题
Interview Question请教一个问题,发两个包子。
问个Array Puzzle题careercup上这道题我竟然没看懂
array of pointers to functions贡献两道google面试题
以前能过的leetcode 3sum, 现在fail了, 求助(时间超出了)Extension problem of finding intersection of two sorted array
A simple google interview questionLRU Cache class:有没有面试可用的精简一些的Sample Code
[a9面经] print x,y,z攒人品之facebook电面面经
CS algorithm question请问一个面试题
相关话题的讨论汇总
话题: ele话题: rear话题: front话题: sum话题: pointer