Leetcode 16. 3Sum Closest

Tag: Array

Description: Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

 

  零碎知识:

  新学了Java中求绝对值这个函数:Math.abs(),括号里的参数可以是表达式

 

  算法方面:

  一定要掌握将一个数组先进行排序(Array.sort()),然后如果有一个数作为target,在对数组元素进行循环相加,将求得的sum和target进行比较的时候,要掌握这个方法:使用两个指针,一个指向数组的开始(start),一个指向数组的末尾(end),因为是排序过的数组,所以一个指向的是最小值,一个指向的是最大值。所以如果sum>target,就让end--,如果sum<target,就让start++。这里start和end是作为数组的下标。

 

posted @ 2018-09-18 14:43  LittleDWADE  阅读(95)  评论(0编辑  收藏  举报