return a + b + c
}
var result = sum(1,2,3) // 1+2+3
// 不限参数个数的
var sum2 = function(){
var res = 0
for(var i=arguments.length - 1i>=0i--){
res += arguments[i]
}
return res
}
var res1 = sum2(1,2,3) // 1+2+3
var res2 = sum2(1,2,3,4,5,6,7) // 1+2+3+4+5+6
class Solution(object):def threeSumClosest(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
nums.sort() #先排序
length = len(nums)
min = float('inf') #最小差值初始化为正无穷
result = 0
for i in range(length - 2): #只需遍历到倒数第三个数
now_num = nums[i]
left = i + 1
right = length - 1
while left < right: #左指针和右指针相遇时结束一轮循环
left_num = nums[left]
right_num = nums[right]
threeSum = now_num + left_num + right_num
error = target - threeSum #目标值和当前三个数和的差
if error == 0: #刚刚好相等,直接返回目标值
return target
elif error > 0: #三个数的和偏小,左指针右移
left += 1
if error < min:
min = error
result = threeSum
else: #三个数的和偏大,右指针左移
right -= 1
if -error < min:
min = -error
result = threeSum
return result