# 2 暴力

``````int temp = nums[0]+nums[1]+nums[2];
for(int i=0;i<nums.length;++i)
for(int j=i+1;j<nums.length;++j)
for(int k=j+1;k<nums.length;++k)
{
int temp1 = nums[i]+nums[j]+nums[k];
if(Math.abs(temp-target) > Math.abs(temp1-target))
{
temp = temp1;
if(temp == target)
return target;
}
}
return temp;
``````

# 3 O(n2)

``````Arrays.sort(nums);
int t1 = nums[0]+nums[1]+nums[2];
for(int i=0;i<nums.length-2;++i)
{
int left = i+1;
int right = nums.length-1;

while(left < right)
{
int t2 = nums[i]+nums[left]+nums[right];
if(t2 == target)
return target;
else if(t2 > target)
--right;
else
++left;
if(abs(t1-target) > abs(t2-target))
{
t1 = t2;
}
}
}
return t1;
``````

# 4 冲击2ms

## 4.1 手写快排

（原理就不多说了，手写快排还是稍微有那么一点难度的...）

``````public void qs(int [] nums,int l,int r)
{
if(l < r-1)
{
int t = l;
int ll = l+1;
int rr = r-1;
int temp;
while(true)
{
while(t < rr && nums[t] < nums[rr])
--rr;
if(t < rr)
{
temp = nums[rr];
nums[rr] = nums[t];
nums[t] = temp;
t = rr--;
}
else
break;
while(ll < t && nums[ll] < nums[t])
++ll;
if(ll < t)
{
temp = nums[ll];
nums[ll] = nums[t];
nums[t] = temp;
t = ll++;
}
else
break;
}
qs(nums,l,t);
qs(nums,t+1,r);
}
}
``````

``````while(ll < rr && ...)
``````

``````while(t < rr && ...)
while(ll < t && ...)
``````

## 4.2 最大最小剪枝

``````int left = i+1;
int right = nums.length-1;
if(left < right)
{
int min = nums[i] + nums[left] + nums[left+1];
if(min > target)
{
if(abs(min - target) < abs(t1 - target))
t1 = min;
continue;
}
}

int max = nums[i] + nums[right] + nums[right-1];
if(max < target)
{
if(abs(max - target) < abs(t1 - target))
t1 = max;
continue;
}
``````

github