I am running one very easy code to solve https://leetcode.com/problems/3sum-closest/
However, one problem encountered me for a long time. I searched google but I think I didn't find the answer. I even tried to run it in some online simulator, same problem. I'd appreciate if you can help me.
This code is not correct, I just want to make it run first. the name is try.cpp now
My source code:
#include<algorithm>
#include<limits.h>
#include<iostream>
#include<vector>
using namespace std;
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(),nums.end());
int ll=nums.size();
int dif=INT_MAX;
int res=INT_MAX;
for(int i = 0;i<ll-2;i++){
int l=i+1,r=ll-1;
while(l<r){
int newdif=nums[l]+nums[r]+nums[i]-target;
if(abs(newdif)<dif){
dif=abs(newdif);
res=nums[l]+nums[r]+nums[i];
}
if(newdif<0){
l+=1;
}else if(newdif>0){
r-=1;
}else{
break;
}
}
}
return res;
}
public:
int main(int argc, char* argv[])
{
int a[3]={0,1,2};
vector<int> v(&a[0],&a[2]);
int b=threeSumClosest(v,3);
cout<<b<<endl;
}
};
my error:
Undefined symbols for architecture x86_64:
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXXgrSG.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXXgrSG.o
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status